zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    网格形变
    网格简化
    无法打开包括文件: “QWidget”: No such file or directory
    遇到一个 bug svg 抖动的解决方案
    echarts-gl 遇到一个错误 groupGL 未定义
    鼠标操控三维视角
    鼠标控制3维操作 不知道能不能获得一些灵感
    tensorflow 安装
    Codeforces Round #541 (Div. 2) B.Draw!
    Codeforces Round #541 (Div. 2) A.Sea Battle
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/5666891.html
Copyright © 2011-2022 走看看