Spring事务的开启方式

时间:2025-01-04 13:34:56

1、通过注解方式@Transactional

@Transactional(rollbackForClassName = { "Exception", "RuntimeException" })
public void save(PersonEntity entity) {
personDao.save(entity);
}

2、通过切片方式

<!-- 配置事务传播特性 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 -->
<aop:config>
<aop:pointcut id="all" expression="execution(* com.zhi.service.*.*(..))" />
<aop:advisor pointcut-ref="all" advice-ref="advice" />
</aop:config>