springboot的EnableTransactionManagement这注解是不是多余的?

本文解释了Spring Boot中如何自动配置事务管理,即使未显式添加@EnableTransactionManagement也能使用@Transactional注解。介绍了事务处理的基本流程及关键组件。
问题:

百度说要用事务,需要在启动类加上@EnableTransactionManagement
但是我都没加,一样可以用@Transactional注解来控制事务。

解答

@EnableTransactionManagement是 spring-tx 的注解,不是 spring-boot 的
spring-boot 会自动配置事务,相关的配置在 org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

在自动配置类里已经写好了 @EnableTransactionManagement

https://blog.csdn.net/qq_32370913/article/details/105924209

https://segmentfault.com/q/1010000018830249/a-1020000018837891

事务不生效
https://blog.csdn.net/weixin_45505313/article/details/103284559

springboot2.0中的事务注意事项:
https://www.cnblogs.com/baoyi/p/springboot_transactional.html

@Transactional采用AOP实现的。
在进行方法调用的时候,发现这个方法有事务注解,AOP首先会检测到,然后用代理类采用反射机制进行调用。
  1. 首先调用了CglibAopProxy.intercept()方法。
  2. 接下来调用ReflectiveMethodInvocation.proceed()方法,
  3. TransactionInterceptor.invoke()
  4. TransactionAspectSupport.invokeWithinTransaction()
  5. TransactionAspectSupport.createTransactionIfNecessary()
  6. AbstractPlatformTransactionManager.getTransaction(),创建了一个新的事务。
  7. PlatformTransactionManager 这个接口中定义了三个方法getTransaction创建事务,commit提交事务,rollback 回滚事务。她的实现类是 AbstractPlatformTransactionManager这个。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值