zoukankan      html  css  js  c++  java
  • Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理;

     Spring支持编程式事务管理声明式事务管理两种方式。

     而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional注解。显然基于注解的方式更简单易用。

    但本文主要讲解的是第一种方式,基于tx和aop命名空间的xml配置文件;

    在applicationContext.xml配置文件里添加以下代码:

     引入tx/aop命名空间:

         <!--事务管理者-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
               <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

        <!-- 由Spring 提供的事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="mod*" propagation="REQUIRED" /> <tx:method name="deleteSingleNews" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice>
       <!-- AOP 的切面点--> <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* news.dao.*.*(..))" /> <!--注意:第一个 * 号的后面是有一个空格的--> <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" /> </aop:config>

    注:Spring声明式事务管理要用的jar包:aopalliance-1.0.jar 、aspectjrt.jar 、aspectjweaver.jar 、cglib-nodep-2.1_3.jar;

    expression="execution(* news.dao.*.*(..))" 中几个通配符的含义:
    第一个 *  》》 通配 任意返回值类型
    第二个 *  》》通配 news.dao包下的任意class
    第三个 *  》》 通配 news.dao包下的任意class的任意方法
    第四个 .. 》》 通配 方法可以有0个或多个参数

    总结:

    基于tx/aop命名空间的xml配置文件方式优点:
    1、降低耦合,使容易扩展。
    2、对象之间的关系一目了然。
    3、xml配置文件比注解功能齐全。
    xml配置文件方式缺点:
    1、配置文件配置工作量相对注解来说要大。

  • 相关阅读:
    matlab cell
    matlab linux 快捷键设置——有问题还是要解决
    latex 小结
    TOJ 1258 Very Simple Counting
    TOJ 2888 Pearls
    HDU 1248 寒冰王座
    TOJ 3486 Divisibility
    TOJ 3635 过山车
    TOJ 1840 Jack Straws
    HDU 4460 Friend Chains
  • 原文地址:https://www.cnblogs.com/liguanxing/p/5978712.html
Copyright © 2011-2022 走看看