项目开发过程中遇到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理应由父容器进行初始化以保证事务的一致性,故这里要进行如上处理。
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
spring mvc + spring + hibernate 全注解整合开发视频教程 06.haozip03
主要介绍Spring MVC 的使用注解来配置的使用实例
学习Spring MVC,关于注解的Spring MVC,简单例子,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例
spring mvc + spring + hibernate 全注解整合开发视频教程 12
spring mvc + spring + hibernate 全注解整合开发视频教程 04
这是一个Spring MVC实例 使用MVC注解配置
spring3.0mvc自学教程ppt+注解教程+注解实例+springmybatis教程+项目实例+中文api 初学(自学)着的不二法定,从入门到项目实例深入学习(浅-深)
spring-mvc注解详情 ,网页版介绍注解,如果不好看,可以搜索里面的关键句子,百度就出来原文了,这方便没有网络的朋友学习
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
spring3hibernate4注解式事务管理
spring mvc + spring + hibernate 全注解整合开发视频教程 11
spring mvc + spring + hibernate 全注解整合开发视频教程 03
spring3.0MVC注解(附实例). spring3.0MVC注解(附实例). spring3.0MVC注解(附实例).
Spring3_MVC注解教程.ppt
介绍 Spring 2.5 新增的 Sping MVC 注解功能,讲述如何使用注解配置替换传统的基于 XML 的 Spring MVC 配置。
spring3.0MVC注解:spring3.0MVC注解spring3.0MVC注解spring3.0MVC注解
spring mvc + spring + hibernate 全注解整合开发视频教程 06.haozip02
Spring MVC非注解测试,用于理解spring mvc原理,然后再结合源码分析,更好的理解spring mvc
hibernate-4.3.5 与Spring(Spring MVC4.0.4)注解方式集成 样例源码