Spring-tx.xml
配置思路:
1. 声明事务管理器DataSourceTransactionManager
,并注入数据源dataSource
属性
2.配置事务增强<tx:advice>
,声明事务管理器transaction-manager=""
,默认值为transaction-manager="transactionManager"
,设置事务属性<tx:attributes>
,声明要为哪些方法配置事务<tx:method name=""/>
,以及定义方法的一些属性
3. 配置切面<aop:config>
,首先通过切入点表达式声明切入点位置,然后关联事务通知和切入点
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="transaction" transaction-manager="transactionManager">
<!-- 事务属性 -->
<tx:attributes>
<tx:method name="*"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<!--增删改 -->
<tx:method name="insert*" timeout="5000" rollback-for="java.lang.Exception"/>
<tx:method name="add*" timeout="5000"/>
<tx:method name="update*" timeout="5000"/>
<tx:method name="delete*" timeout="5000"/>
</tx:attributes>
</tx:advice>
<!-- 配置版的事务切面 -->
<aop:config>
<!-- *任意返回值 impl包下 .*任意类 .*任意方法 (..)任意参数 -->
<aop:pointcut id="pointcut" expression="execution(* com.juyss.service.impl.*.*(..))"/>
<!-- advice-ref:关联增强事务 pointcut-ref:关联事务的切入点 -->
<aop:advisor advice-ref="transaction" pointcut-ref="pointcut"/>
</aop:config>
</beans>