新建项目记录下springmvc和spring的事务配置,解决事务不回滚的问题。
问题说明:
Spring容器优先加载由ServletContextListener(applicationContext.xml)产生的父容器,而SpringMVC(spring-mvc.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service。
配置修改:
applicationContext.xml
<!-- 自动扫描dao和service包(自动注入) -->
<!--spring 自动扫描注解的时候,不去扫描@Controller-->
<context:component-scan base-package="com.*">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
spring-mvc.xml
<!-- 添加注解驱动 -->
<!--spring mvc 自动扫描注解的时候,不去扫描@Service-->
<context:component-scan base-package="com.*" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
参考文章:
本文出自 “大程熙的小角落” 博客,请务必保留此出处http://dachengxi.blog.51cto.com/4658215/1718423