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

问题:

百度说要用事务,需要在启动类加上@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这个。

你可能感兴趣的:(SpringBoot)