终于找到全annotation配置springMVC的方法了(事务不失效)
2014-07-15 16:39
337 查看
原理参考:
http://jinnianshilongnian.iteye.com/blog/1762632
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行
这个问题是由于问答上有解决方案
引用
这个问题很经典了
在主容器中(applicationContext.xml),将Controller的注解排除掉
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。
还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。
http://jinnianshilongnian.iteye.com/blog/1762632
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行
这个问题是由于问答上有解决方案
引用
这个问题很经典了
在主容器中(applicationContext.xml),将Controller的注解排除掉
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。
还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。
相关文章推荐
- [转]终于找到全annotation配置springMVC的方法了(事务不失效)
- 终于找到全annotation配置springMVC的方法了(事务不失效)
- 终于找到全annotation配置springMVC的方法了(事务不失效)
- 终于找到全annotation配置springMVC的方法了(事务不失效)
- 终于找到全annotation配置springMVC的方法了(事务不失效)
- annotation配置springMVC的方法了事务不起作用
- [转]annotation配置springMVC的方法了事务不起作用
- annotation配置springMVC的方法了事务不起作用
- 【SpringMVC】annotation配置(事务不失效)
- 关于同时用Spring和SpringMVC配置文件,导致事务失效的分析
- springMVC注解配置事务失效的问题(精)
- spring 在有事务的方法上配置自定义日志切面,事务会失效解决方法
- SpringMvc配置 导致实事务失效
- SpringMVC事务失效的解决方法
- Asp.net中的VS2010中的ashx文件,【转到定义】失效的解决方法!(终于找到原因了)
- springmvc 基于注解的事务配置,事务失效
- SpringMVC事务失效的解决方法
- 面试题整理,英文简介、struts2功能,mybatis优点,jface概念,struts2组件,springmvc机制,配置事务,hiernate查询方法
- 常常遇到VC下面断点失效的问题,终于找到比较简单靠谱的解决方法了!
- spring事务配置在annotation不启作用,请问有什么解决办法或其它配置方法3