您的位置:首页 > 编程语言 > Java开发

【Spring学习】Spring事物管理之aop技术

2015-04-30 15:47 375 查看
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置事务管理器 -->
<bean id="possTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"><ref bean="dataSource" /></property>
</bean>
<!-- 事物管理名称: 配置事务的传播特性,配置一个切入点, 属面向切面编程, pointcut就是为了区分哪些方法需要被"加强" -->
<tx:advice id="possTxAdvice" transaction-manager="possTransactionManager">
<tx:attributes>
<tx:method name="*RdTx" propagation="REQUIRED" rollback-for="com.common.item.base.exception.TransactionException"/>
<tx:method name="*SpTx" propagation="SUPPORTS" rollback-for="com.common.item.base.exception.TransactionException"  />
<tx:method name="*NsTx" propagation="NOT_SUPPORTED" />
<tx:method name="*RnTx" propagation="REQUIRES_NEW" rollback-for="com.common.item.base.exception.TransactionException" />
</tx:attributes>
</tx:advice>
</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 -->

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


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

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

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

第二个*代表所有的类

第三个*代表类所有方法

最后一个..代表所有的参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: