在开发过程中,spring的申明式事物可能出现,在同样的代码,在测试类中可实现事物回滚效果,正式程序中事物回滚效果失效。
这一块可能就是在事物扫描过程中@transaction注解失效的原因。只要修改spring配置文件即可,将controller和service注解分开扫描。
spring-mvc.xml,这里只扫描@controller注解
<context:component-scan base-package="com.scabs" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
spring-context.xml这个配置文件中,不扫描@Controller注解
<context:component-scan base-package="com.scabs">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
以上配置,即可解决spring事物注解的失效的问题。