spring结合spring mvc下用注解式事务不生效

来源:互联网 发布:淘宝热卖场女装 编辑:程序博客网 时间:2024/05/12 23:10

      项目开发过程中遇到spring注解事务不生效的情况,项目使用的是spring mvc、spring、mybatis 。bean都是用spring的注解管理的,事务也是用的注解式。经过几番折腾发现问题,特此记录解决方法如下:

 一、在applicationContext.xml文件中扫描bean时排除Controller。

<context:component-scan base-package="com.newmode.fcm"><context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/><context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>

二、在springMvc.xml文件中扫描Controller时排除Service。

<context:component-scan base-package="com.newmode.fcm.web"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/></context:component-scan>


三、在要事务控制的方法上加上@Transactional注解。


      运行,nice,完美控制,原因如下:
      因为spring是由org.springframework.web.context.ContextLoaderListener加载,spring mvc是由org.springframework.web.servlet.DispatcherServlet加载,互不干扰,如果spring mvc中不排除@Service他就会一并把@Service扫描装配进来,而该@Service理应由父容器进行初始化以保证事务的一致性,故这里要进行如上处理。 


0 0