该文章是以基于<aop:advisor>方法面向方面编程为基础的,有关详细内容,请看
http://blog.csdn.net/yi16881/archive/2010/06/09/5659704.aspx
本文讲解xml文档中tx:aop配置方法
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
//阴影部分配置一定不能忘记,否则不能识别aop和tx
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>system</value>
</property>
<property name="password">
<value>abcd</value>
</property>
</bean>
//该部分为数据源配置,不再多讲
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean>
//增加事物管理类DataSourceTransactionManager.class
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* insertData(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/>
</aop:config>
//aop部分在该文中开头部分的链接中有详解
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insertData"/>
</tx:attributes>
</tx:advice>
//tx:method元素属性如下表
<bean id="testDao" class="com.sias.TestDao">
<constructor-arg><ref local="datasource"/></constructor-arg>
</bean>
</beans>
tx:method属性如下表
事务属性 | 是否必须 | 默认值 | 描述 |
name | 是 | 与事物相关联的方法,可以用通配符“*” | ru get* handler |
propagation | 否 | REQUIRED | 事务传播行为,可选值为: required,supports,mandatory, requires_new,not_supported, never,nested |
isolation | 否 | DEFAULT | 事务隔离级别,可选值为 default,read_uncommitted,read_committed, repeatable_read,serializable |
timeout | 否 | -1 | 事务超时的时间(以秒为单位),如果设置为-1, 事务超时的时间由底层的事物系统决定 |
read-only | 否 | false | 事务是否只读 |
rollback-for | 否 | 所有运行期异常回滚 | 触发事务回滚的Exception,可以设置多个,以 都好分开,如:"Exception1,Exception2" |
no-rollback-for | 否 | 所有检查异常不回滚 | 不触发事务回滚的Exception,可以设置多个,以 逗号分开,如:"Excepition1,Exception2" |