【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 包下所有的类的所有方法。。

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

第二个*代表所有的类

第三个*代表类所有方法

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

posted @ 2015-04-30 15:47  会开窗的金莲  阅读(225)  评论(0编辑  收藏  举报