Spring MVC中,事务是否可以加在Controller层
来源:互联网 发布:畅游无限浏览器 mac版 编辑:程序博客网 时间:2024/06/05 18:24
Spring MVC中,事务是否可以加在Controller层
一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Controller层可不可以。我一直试图证明事务不止可以加在Service层,还可以加在Controller层,但是没有找到有力的论据来支持我这个想法,搞得我一度认为事务只能加在Service层,直到我读过spring官方文档并实践之后,我知道我的想法是对的。
在spring-framework-reference.pdf文档中有这样一段话:
<tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services.
这句话的意思是,<tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解,如果你把它放在Dispatcher的应用上下文中,它只检查控制器上的@Transactional注解,而不是你services上的@Transactional注解。
于是,我将事务配置定义在Spring MVC的应用上下文(*-servlet.xml)中,将@Transactional注解打在Controller上,终于事务起作用了。
综上,在Spring MVC中,事务不仅可以加在Service层,同样也可以加在Controller层(虽然不推荐这么做,但至少满足了我的好奇心,(*^__^*) 嘻嘻……)。
注意: Controller层只支持 @Transactional 注解式事务!
原文:http://blog.csdn.net/mmm333zzz/article/details/45288061
- Spring MVC中,事务是否可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- 在Spring MVC中,事务可以加在Controller层
- spring在controller层声明事务
- 关于spring在controller层的事务操作
- 在Spring MVC controller中配切点
- spring mvc 控制器(Controller)中可以返回的类型
- spring mvc 给Controller添加事务
- spring-mvc 非 controller 层获取HttpServletRequest
- spring mvc Controller层(学习一)
- Spring+Hibernate+MVC:Controller层中引入@Transaction对Service层设计的简化
- spring MVC中controller层和service层的junit4测试
- 在spring MVC的controller中获取ServletConfig
- spring mvc,在controller中获取属性文件的值
- 在spring mvc应用中创建第一个Controller
- Qt之Qwt初探(二)(完)
- 网狐荣耀棋牌搭建、架设技巧 游戏房间连接失败处理
- java多线程基础知识(一)
- java泛型编程,学习笔记
- mysql 日期加减天数
- Spring MVC中,事务是否可以加在Controller层
- Nginx upstream的5种权重分配方式(转)
- LeetCode 73. Set Matrix Zeroes
- linux下iperf的安装教程
- 域服务篇 06. Windows10 加入域 ❀ Windows Server 2016
- MD5 单向加密
- Python-调试
- datatables 小demo
- nginx需要注意的地方9