当前位置:   article > 正文

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

@transactional执行原理

注意:如果想要理解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)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/525704
推荐阅读
相关标签
  

闽ICP备14008679号