首先扫描配置要对:
原理:因为spring容器和spring-mvc是父子容器,spring容器会先加载,如果此时扫描了Controller,但未扫描到Service。
spring事务配置文件还有上下文都是通过org.springframework.web.context.ContextLoaderListener加载的,而spring MVC的action是通过org.springframework.web.servlet.DispatcherServlet加载的 。
web是先启动ContextLoaderListener后启动DispatcherServlet 在ContextLoaderListener加载的时候Controller并没在容器中,所以现在使用AOP添加事务或者扫描注解都是无用的。
结论:让spring扫描注册Service实现类,让MVC扫描注册Controller,此时spring父容器已经注册Service为Bean,此时事务可以得到正常配置。
扫描配置如下:
spring-context.xml
<context:component-scan base-package="com.cn">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
spring-mvc.xml
<context:component-scan base-package="com.cn.*.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
mvc扫描controller,spring扫描其他bean。
事务配置如下:
spring-context.xml
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
< tx:annoation-driven/> 只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解。
由于我们将配置加入到了spring的配置文件中,而spring扫描了除controller外的包,目前service处于应用的上下文,所以service的事务已经生效,而要使controller的事务生效,你要把它放在Dispatcher的应用上下文中(mvc配置文件中),它就会检查controller上的@Transactional注解。
所以在spring-mvc.xml中加入如下配置即可开启controller事务:
<tx:annotation-driven/>
将@Transactional注解写在Controller上,事务起就可以作用,此时已经能回滚RuntimeException;如果想要回滚其他异常,可以指定@Transactional(rollbackFor=Exception.class)。