Spring Boot中不需要加 @EnableTransactionManagement 来开启事务
1 Spring中 纯XML 配置事务
1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 2 <property name="dataSource" ref="pooledDataSource"/> 3 </bean> 4 <aop:config> 5 <aop:pointcut expression="execution(* cn.yuanyu.crud.service..*(..))" id="txPoint"/> 6 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> 7 </aop:config> 8 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 9 <tx:attributes> 10 <tx:method name="*"/> 11 <tx:method name="get*" read-only="true"/> 12 </tx:attributes> 13 </tx:advice>
2 Spring中 XML+注解 配置事务
一般xml里面配置粗粒度的控制,然后使用注解
1 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 2 <property name="dataSource" ref="pooledDataSource"/> 3 </bean> 4 <aop:config> 5 <aop:pointcut expression="execution(* cn.yuanyu.crud.service..*(..))" id="txPoint"/> 6 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> 7 </aop:config> 8 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 9 <tx:attributes> 10 <tx:method name="*"/> 11 <tx:method name="get*" read-only="true"/> 12 </tx:attributes> 13 </tx:advice> 14 <!-- 15 --> 16 <tx:annotation-driven transaction-manager="transactionManager"/>
3 Spring中 纯注解 配置事务
1 @Configuration //声明配置类 2 @MapperScan("cn.yuanyu.tx.mapper") 3 @EnableTransactionManagement // 开启事务注解,等同于配置文件<tx:annotation-driven/> 4 public class MybatisPlusConfiguration {
4 Spirng Boot 中无需做任何事情
~/org/springframework/boot/spring-boot-autoconfigure/2.3.0.RELEASE/spring-boot-autoconfigure-2.3.0.RELEASE.jar!/META-INF/spring.factories
1 # Auto Configure 2 org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,
1 /** 2 * Auto-configuration for Spring transaction. 3 */ 4 @Configuration(proxyBeanMethods = false) 5 @ConditionalOnClass(PlatformTransactionManager.class) 6 @AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class}) 7 @EnableConfigurationProperties(TransactionProperties.class) 8 public class TransactionAutoConfiguration { 9 //... 10 @Configuration(proxyBeanMethods = false) 11 @ConditionalOnBean(TransactionManager.class) 12 @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class) 13 public static class EnableTransactionManagementConfiguration { 14 @Configuration(proxyBeanMethods = false) 15 @EnableTransactionManagement(proxyTargetClass = false) //这里其实以及加了 16 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false) 17 public static class JdkDynamicAutoProxyConfiguration {} 18 @Configuration(proxyBeanMethods = false) 19 @EnableTransactionManagement(proxyTargetClass = true) //这里其实以及加了 20 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true) 21 public static class CglibAutoProxyConfiguration {} 22 } 23 }
同理不需要加 @EnableAspectJAutoProxy 来开启aop
1 @Configuration(proxyBeanMethods = false) 2 @ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true) 3 public class AopAutoConfiguration { 4 @Configuration(proxyBeanMethods = false) 5 @ConditionalOnClass(Advice.class) 6 static class AspectJAutoProxyingConfiguration { 7 @Configuration(proxyBeanMethods = false) 8 @EnableAspectJAutoProxy(proxyTargetClass = false) 9 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false) 10 static class JdkDynamicAutoProxyConfiguration {} 11 @Configuration(proxyBeanMethods = false) 12 @EnableAspectJAutoProxy(proxyTargetClass = true) 13 @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true) 14 static class CglibAutoProxyConfiguration {} 15 } 16 }
转载:https://blog.csdn.net/qq_40794973/article/details/106597952