微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMVC 全局异常处理

(一)、SpringMVC全局异常快速入门 

 

 

 

 (二)、Spring及SpringMVC扫描包隔离及配置文件优化 

          1、删除applicationContext-datasource.xml文件里context:component-scan        

 <context:component-scan base-package="com.mmall" annotation-config="true"/>

          2、applicationContext.xml文件          

<!-- 扫描com.mmall包下的注解,就可以很方便的在类中进行注入-->
    <!-- 排除controller注解-->
    <!-- ApplicationContext.xml  是spring 全局配置文件,用来控制spring 特性的-->
    <!-- dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view  Controller扫描放到这里,其他扫描放到ApplicationContext.xml-->

    <context:component-scan base-package="com.mmall" annotation-config="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

           3、dispatcher-servlet.xml文件

           

<!--  只扫描controller注解,其他bean的applicationContext.xml  扫描放到 只要有方法使用了@Controller,就会扫描到-->
    <!-- use-default-filter 关闭认扫描-->
    <context:component-scan base-package="com.mmall.controller" annotation-config="true" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

 

备注:

转载地址:https://www.cnblogs.com/parryyang/p/5783399.html

applicationContext.xml和dispatcher-servlet.xml的区别

在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢?

  Spring 官方文档介绍如下:

Spring lets you define multiple contexts in a parent-child hierarchy.  
    The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.  
    The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).  
    Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.  
    All Spring MVC controllers must go in the spring-servlet.xml context.  
    In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

可见, applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。

  1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。

  2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。

  不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。

  ----------------------------------------------------------------------------------------------------------------------------------------

  applicationContext.xml  是spring 全局配置文件,用来控制spring 特性的

  dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view

 

Contronller bean放在dispatcher-servlet.xml,其他bean放在applicationContext.xml 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。