zoukankan      html  css  js  c++  java
  • 关于@service、@controller和@transactional 在spring中的位置说明

    Spring容器优先加载由ServletContextListener(对应applicationContext.xml)产生的父容器,而SpringMVC(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Controller进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将是原样的无事务处理能力的Service。所以应在spring mvc配置文件中,不包括service的扫描。

    spring mvc:

    <context:component-scan base-package="com.pamirs.pradar.controller">
            <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:component-scan base-package="com.pamirs.pradar.controller">  
            <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
        </context:component-scan>

     添加@transactional主要注意的问题:

    <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上,终于事务起作用了。

  • 相关阅读:
    HDU 1042 N!
    linux上安装wps办公软件
    《UNIX环境高级编程》笔记--环境变量
    【StatLearn】统计学习中knn算法实验(2)
    Asp.Net验证控件浅析
    斜率小于0的连线数量-归并排序
    Printk与sched_clock_init的一点分析
    【数字图像处理】使用kmeans算法对TrueColor图片进行优化
    将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
    python中os/sys/platform模块区别
  • 原文地址:https://www.cnblogs.com/zhouj-happy/p/6129308.html
Copyright © 2011-2022 走看看