版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyzf0413/article/details/62423325
在Spring配置文件applicationContext.xml中以AOP方式配置事务
<!-- 事务配置 在需要事务管理的地方加@Transactional 注解或者AOP进行事务处理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 注入事务策略 -->
<tx:method name="query*" read-only="true" />
<tx:method name="insert*" />
<tx:method name="update*" />
<tx:method name="del*" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP切点 -->
<aop:config>
<!-- 配置AOP切点策略 -->
<aop:pointcut expression="execution(public * service.impl.*.*(..))" id="service"/>
<!-- 注入切点,切点策略 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>
使用@Transactional 注解配置事务,则需要给每个方法都加上注解,势必太麻烦。则以使用aop面向切面编程思想管理事务;
<tx:method/>标签的属性:
name:方法名的匹配模式,通知根据该模式寻找匹配的方法。
propagation:设定事务定义所用的传播级别。
isolation:设置事务的隔离级别。
timeout:指定事务的超时(秒)。
read-only:该属性为true指示事务是只读的
no-rollback-for:以逗号分隔的异常类的列表,目标方法可以跑出这些异常而不会导致通知执行回滚
rollback-for:以逗号分隔的异常类的列表,当目标方法跑出这些异常时会导致通知执行回滚。默认情况下,该列表为空,因此不在no-rollback-for列表中的任何运行时异常都会导致回滚。