zoukankan      html  css  js  c++  java
  • 【Spring学习】Spring事物管理之aop技术

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4     xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xsi:schemaLocation="
     6             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     7             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
     8             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
     9      <!-- 配置事务管理器 -->
    10     <bean id="possTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    11         <property name="dataSource"><ref bean="dataSource" /></property>
    12     </bean>
    13     <!-- 事物管理名称: 配置事务的传播特性,配置一个切入点, 属面向切面编程, pointcut就是为了区分哪些方法需要被"加强" -->
    14     <tx:advice id="possTxAdvice" transaction-manager="possTransactionManager">
    15         <tx:attributes>
    16             <tx:method name="*RdTx" propagation="REQUIRED" rollback-for="com.common.item.base.exception.TransactionException"/>
    17             <tx:method name="*SpTx" propagation="SUPPORTS" rollback-for="com.common.item.base.exception.TransactionException"  />
    18             <tx:method name="*NsTx" propagation="NOT_SUPPORTED" />
    19             <tx:method name="*RnTx" propagation="REQUIRES_NEW" rollback-for="com.common.item.base.exception.TransactionException" />
    20         </tx:attributes>
    21     </tx:advice>
    22 </beans>

    <tx:advice/>配置详解

    <tx:advice>:事务通知定义,用于指定事务属性,其中“transaction-manager”属性指定事务管理器,并通过< tx:attributes >指定具体需要拦截的方法;

    <tx:method name="save*">:表示将拦截以save开头的方法,被拦截的方法将应用配置的事务属性:propagation="REQUIRED" 表示传播行为是Required,isolation="READ_COMMITTED"表示隔离级别是提交读;

     rollback-for需要触发回滚的异常定义,以“,”分割,默认任何RuntimeException 将导致事务回滚,而任何Checked Exception 将不导致事务回滚;异常名字定义和TransactionProxyFactoryBean中含义一样

     no-rollback-for不被触发进行回滚的 Exception(s);以“,”分割;异常名字定义和TransactionProxyFactoryBean中含义一样;

     timeout事务超时时间设置,单位为秒,默认-1,表示事务超时将依赖于底层事务系统;

     read-only事务只读设置,默认为false,表示不是只读;

    <!-- 配置事务拦截器拦截哪些类的哪些方法,一般设置成拦截Service -->

    1     <aop:config>
    2         <aop:pointcut id="cncrowdServiceMethod"
    3             expression="execution(* com.cncrowd.mobile.service.*.*(..))" />
    4         <aop:advisor advice-ref="possTxAdvice" pointcut-ref="cncrowdServiceMethod" />
    5     </aop:config>

    execution(* com.cncrowd.mobile.service.*.*(..))

    这样写应该就可以了 这是com.cncrowd.mobile.service 包下所有的类的所有方法。。

    第一个*代表所有的返回值类型 

    第二个*代表所有的类

    第三个*代表类所有方法

    最后一个..代表所有的参数。

  • 相关阅读:
    jtl转化成CSV格式的聚合报告
    JMeter--自动生成测试报告
    centos7中没有安装ifconfig命令的解决方法
    Tomcat 安装
    JDK安装
    iOS学习笔记(九)—— xml数据解析
    Wyn Enterprise用户行为日志分析
    详解Wyn仪表板图表中的趋势线
    报表中的条形码和二维码生成
    Wyn Enterprise连接达梦DM8数据库教程
  • 原文地址:https://www.cnblogs.com/yantz/p/4469090.html
Copyright © 2011-2022 走看看