spring 声明式事务 @Transactional 运行原理

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

注意如果想要理解spring 的声明式事务必须先理解AOP 的原理。

一、spring注册 InfrastructureAdvisorAutoProxyCreator

  1. 通过 @EnableTransactionManagement 可以看到先把TransactionManagementConfigurationSelector通过@Import注册到spring。同时注意mode 的默认值是 PROXY

在这里插入图片描述

  1. TransactionManagementConfigurationSelector间接 实现了 ImportSelector 接口所以就有了将某个bean引入spring 的能力。会把 AutoProxyRegistrarProxyTransactionManagementConfiguration 引入到spring

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1gMSdOui-1674915716908)(/Users/liuyanfei/work/idea20182/workspace/mywork/lyf-iron-man-component/iron-man-springbase/iron-man-spring1/src/md/4.声明式事务原理/image-20230128212508609.png)]

  1. AutoProxyRegistrar 实现了ImportBeanDefinitionRegistrar所以会用registry将bean注入到spring中。此处还是使用PROXY模式下的逻辑

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zVxHb9bG-1674915716909)(/Users/liuyanfei/work/idea20182/workspace/mywork/lyf-iron-man-component/iron-man-springbase/iron-man-spring1/src/md/4.声明式事务原理/image-20230128212719294.png)]

  2. 最终 registry会将 InfrastructureAdvisorAutoProxyCreatororg.springframework.aop.config.internalAutoProxyCreator为name注入到spring中。InfrastructureAdvisorAutoProxyCreator 和AOP 的AnnotationAwareAspectJAutoProxyCreator.class一样也实现了 BeanPostProcessor接口成为了spring 的 BeanPostProcessor后置处理器

在这里插入图片描述

二、特别注意

  1. 此处把 InfrastructureAdvisorAutoProxyCreatororg.springframework.aop.config.internalAutoProxyCreator为name注入到spring中。和 AOP 把 AnnotationAwareAspectJAutoProxyCreator.class注入到spring使用的BeanName是一样的。下面的两个调用会进入到同一个方法中。那么如果一个方法即使用 声明式事务、又使用 AOP 会如何?

在这里插入图片描述

  1. 在初始化这个类的时候就会定义好优先级。AOP的 AnnotationAwareAspectJAutoProxyCreator优先级最高声明式事务 的 InfrastructureAdvisorAutoProxyCreator优先级最低。

在这里插入图片描述

  1. 具体的体现。如果一个方法即使用 声明式事务、又使用 AOP 。那么会使用AOP 的AnnotationAwareAspectJAutoProxyCreator.class在AOP的后置处理器处理连接链
    在这里插入图片描述

三、InfrastructureAdvisorAutoProxyCreator分析

在这里插入图片描述

InfrastructureAdvisorAutoProxyCreator 是间接实现了 BeanPostProcessor接口。

四、获取代理对象

与AOP 获取代理对象是一样的。

五、ProxyTransactionManagementConfiguration 分析

一、2 中除了往spring注册 AutoProxyRegistrar 还往spring中注册了 ProxyTransactionManagementConfiguration

在这里插入图片描述

  1. transactionAttributeSource() 主要是用来解析事务注解的属性

在这里插入图片描述

在这里插入图片描述

  1. transactionInterceptor(TransactionAttributeSource transactionAttributeSource)方法用来设置 动态代理时候的连接链并把 TransactionManager事务管理器设置进去到时好做 commit 或者 rollback

六、代理链

声明式事务的代理链只有一个链所以没啥好分析的。与AOP的代理链一起分析更带劲。

二、特别注意中可知如果一个方法即使用 声明式事务、又使用 AOP那么最终会使用AOP的后置处理器。所以说最后的代理对象也是使用同一个代理对象。所以说当调用 目标方法时进入到invoke 方法后可以看到拦截链。

在这里插入图片描述

很明显声明式事务 和AOP 的拦截链是混在一起的。而且是排在第一位的

在这里插入图片描述

七、声明式事务的代理链

框中的方法其实 是在 各自的方法中调用 TransactionManager().commit 、 TransactionManager().rollback 等方法。与编程式事务无异。所以说声明式事务的底层是编程式事务

在这里插入图片描述

八、AOP 与 声明式事务 的调用过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-APOoXPke-1674915716914)(/Users/liuyanfei/work/idea20182/workspace/mywork/lyf-iron-man-component/iron-man-springbase/iron-man-spring1/src/md/4.声明式事务原理/5.aop实现原理下--代理过程.png)]

九、AOP 与 声明式事务 的调用逻辑

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fulqoOHR-1674915716915)(/Users/liuyanfei/work/idea20182/workspace/mywork/lyf-iron-man-component/iron-man-springbase/iron-man-spring1/src/md/4.声明式事务原理/6.aop调用链.png)]

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring