• 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、配置文件配置工作量相对注解来说要大。

  • 相关阅读:
    Cocos2dx-demo演示项目:Part2
    利用Python抓取亚马逊评论列表数据
    Cocos2dx-demo演示项目:Part1
    正则表达式匹配原理
    js正则函数中test和match的区别
    【别人家的孩子系列之】浅析正则表达式—(原理篇)
    JS 获取浏览器窗口大小
    javascript的insertBefore、insertAfter和appendChild简单介绍
    javascript 限制字符串字数换行 带BUG
    一行一行分析JQ源码学习笔记-06
  • 原文地址:https://www.cnblogs.com/liguanxing/p/5978712.html
Copyright © 2020-2023  润新知