版权声明:本文为博主ExcelMann的原创文章,未经博主允许不得转载。
10、声明式事务
作者:ExcelMann,转载需注明。
10.1、回顾事务
要么成功,要么失败。
10.2、spring中的事务管理
- 声明式事务:AOP方式,不影响原来的代码
- 编程式事务
采用声明式事务:
- 第一步:配置声明式事务
- 第二步:创建一个事务的切面
- 第三次:配置aop,选择切入点,选择环绕方式,引入切面
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="datasource" />
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--给哪些方法配置事务-->
<!--新的属性propagation:配置事务的传播特性,默认为REQUIRED-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务的aop切入-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.excelman.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
备注:
在整合了SSM之后,事务的管理将交由给Spring中的spring-service.xml负责(即service层的配置文件负责)。