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

      项目开发过程中遇到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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值