1.spring容器与springMVC容器冲突(spring version 4.0.2.RELEASE)
SpringMVC容器是Spring容器的一个子容器,它同样能够初始化实体类。由于SpringMVC容器的初始化是在Spring容器初始化之后,所以它会替换Spring中已经存在的类,这样可能会导致冲突。
(1)只在applicationContext.xml中配置如下
<context:component-scan base-package="com.pz" />
启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效
(2)只在spring-servlet.xml中配置上述配置
启动正常,请求也正常,但是事物失效,也就是不能进行回滚
(3)在applicationContext.xml和spring-servlet.xml中都配置上述信息
启动正常,请求正常,也是事物失效,不能进行回滚
因此将action单独配置在一个包中,如果已有项目无法改变,则用以下配置
<!--spring容器 -->
<context:component-scan base-package="com.pz">
<!-- 不扫描@Controller -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- springMVC容器 -->
<context:component-scan base-package="com.pz.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
引用:因为spring的context是父子容器,所以会产生冲突,Controller会进步前辈行扫描装配,而此时的Service还没有进行事务的加强处理惩罚,获得的将是原样的Service(没有经过事务加强处理惩罚,故而没有事务处理惩罚才能) ,最后才是applicationContext.xml中的扫描设备进行事务处理惩罚