解决Spring和SpringMVC扫描注解类的冲突问题

最正确的配置方式:
使用Spring配置文件只扫描@Service,@Repository的bean ,使用SpringMVC配置文件只扫描@Controller

如果两边都加上<context:component-scan base-package="com.skyedu"></context:component-scan>这样配置是错误的,因为会扫描bean两次

 

即 

首先通过exclude-filter 进行黑名单过滤;

然后通过include-filter 进行白名单过滤;

否则默认排除(use-default-filters默认为true)

 

还有注意:配置了SpringMVC配置了<context:component-scan>就不用再配置<context:annotation-config />(启用SpringMVC注解),因为前者已经包含了后者。

 

 

(Spring配置)在springContext.xml中,将controller,adviceController注解给去掉  (use-default-filters默认值是true,会扫描该包和子包的所有类) 

[html] 
  1. <context:component-scan base-package="com.xxx.xxx" use-default-filters="false">  
  2.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  3.     <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice/>  
  4. </context:component-scan>  

 

(SpringMVC配置)在springmvc.xml中,将Service注解给去掉    (use-default-filters默认值是true,会扫描该包和子包的所有类) 

[html] 
  1. <context:component-scan base-package="com.xxx.xx" use-default-filters="false">
  2.     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service/>
  3.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  4.     <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice/>  
  5. </context:component-scan>  

因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。

还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。

也可以用直接扫描的方式:

   直接扫描比较省事,但是事务回得不到处理,所以在具体的层面上还需要加入注解去声明事务,比如在dao层和service层加入@Transactional

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

几种不同配置的测试:

(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com" />
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效,出现404错误
(2)只在springmvc.xml中配置

<context:component-scan base-package="com" />

启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和springmvc.xml中都配置

<context:component-scan base-package="com" />
启动正常,请求正常,也是事物失效,不能进行回滚
(4)在applicationContext.xml中配置如下
<context:component-scan base-package="com.service" />
在springmvc.xml中配置如下
<context:component-scan base-package="com.action" />

或者按最正确的配置applicationContext.xml,将Controller的注解排除掉 ,springmvc.xml,将Service注解给去掉 
此时启动正常,请求正常,事物也正常了。

posted @ 2018-03-26 10:30  ƃuədnxıəʍ  阅读(438)  评论(0编辑  收藏  举报