码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Spring及Spring boot 第四章-第二节 Spring声明式事务管理 解析@Transactional


    Spring及Spring boot 第四章-第二节 Spring声明式事务管理 解析@Transactional

          • (1) 获取Advisor——BeanFactoryTransactionAttributeSourceAdvisor
          • (2)判断Advisor是否应用于该Bean
            • AopUtils#canApply——非常重要的场景
            • 2.1 粗筛 ——判断Bean类中有没有@Transactional注解
            • 2.2 真正的筛选
              • TransactionAttributeSourcePointcut#matches ——这里有最重要的一步,拿到通过tas拿到我们的BeanDefinition
              • 根据TransactionAttributeSource拿到@Transactional包装的所有事务信息
          • 附录
            • 解析@Transactional转变过程
              • Advisor——Pointcut
              • Pointcut——MethodMatcher
              • MethodMatcher(Pointcut本身)—— TransactionAttributeSource

    这个过程可以和@Aspect 实现的AOP来进行对比

    SpringAop获取Advisor

    啊啊

    (1) 获取Advisor——BeanFactoryTransactionAttributeSourceAdvisor

    @Transactional和@Aspect获取Advisor的不同之处在于它的Advisor一开始便是以Bean的形式存在,不需要通过解析@Aspect注解来将方法转化为Advisor

    (2)判断Advisor是否应用于该Bean

    在Spring声明式事务管理解析@Transactional注解的过程中会将它的信息转化为TransactionAttribute对象,

    首先进行判断,BeanFactoryTransactionAttributeSourceAdvisor不是IntroductionAdvisor类型然后开始判断canApply,这是重点

    IntroductionAdvisor的意义是实现AOP引入功能,和切点功能还不太一样。这里BeanFactoryTransactionAttributeSourceAdvisor当然是作为PointCutAdvisor
    参考链接跟着小马哥学系列之 Spring AOP
    在这里插入图片描述

    在这里插入图片描述

    AopUtils#canApply——非常重要的场景

    它做了两件事

    1. 判断这个目标类上有没有@Transactional注解,这样就可以知道事务相关的Advisor(BeanFactoryTransactionAttributeSourceAdvisor)能不能应用于该Bean,方便做之后的处理
    2.  如果有,之后就进行真正的@Transactional注解解析,这里是非常重要的点(获取TransactionDefinition)
    
    • 1
    • 2
     public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
            Assert.notNull(pc, "Pointcut must not be null");
            if (!pc.getClassFilter().matches(targetClass)) {
                return false;
            } else {
                MethodMatcher methodMatcher = pc.getMethodMatcher();
                if (methodMatcher == MethodMatcher.TRUE) {
                    return true;
                } else {
                    IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
                    if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
                        introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher)methodMatcher;
                    }
    
                    Set<Class<?>> classes = new LinkedHashSet();
                    if (!Proxy.isProxyClass(targetClass)) {
                        classes.add(ClassUtils.getUserClass(targetClass));
                    }
    
                    classes.addAll(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
                    Iterator var6 = classes.iterator();
    
                    while(var6.hasNext()) {
                        Class<?> clazz = (Class)var6.next();
                        Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);
                        Method[] var9 = methods;
                        int var10 = methods.length;
    
                        for(int var11 = 0; var11 < var10; ++var11) {
                            Method method = var9[var11];
                            if (introductionAwareMethodMatcher != null) {
                                if (introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) {
                                    return true;
                                }
                            } else if (methodMatcher.matches(method, targetClass)) {
                                return true;
                            }
                        }
                    }
    
                    return false;
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    2.1 粗筛 ——判断Bean类中有没有@Transactional注解

    这步属于粗筛,一方面我们都知道@Transactional注解是可以加到方法或者类上的,粗筛会通过把这个类上的所有注解都遍历一下看有没有Transactional.Class类型的注解,只做这一件事,返回个true或者false,不属于真正对@Transactional的处理

    TransactionAttributeSourceClassFilter#matches 
    
    • 1

    这里就需要通过TransactionAttributeSource的实现类(AnnotationTransactionAttributeSource)来实现

    在这里插入图片描述

    AnnotationTransactionAttributeSource#isCandidateClass(clazz)

    2.2 真正的筛选

    重点
    methodMatcher.matches(method, targetClass)
    在这里插入图片描述
    在这里插入图片描述
    从附录中可以看到实现的就是TransactionAttributeSourcePointcut#matches

    TransactionAttributeSourcePointcut#matches ——这里有最重要的一步,拿到通过tas拿到我们的BeanDefinition

    一定要对命名有敏感,TransactionAttributeSource就是TransactionAttribute源,也就是说,所有的TransactionAttribute保存到TransactionAttributeSource相关的实现类中,TransactionAttribute就是我们事务功能实现中的TransactionDefinition组件。

    缓存

    在这里插入图片描述
    不出意外拿到@Transactional注解 事务信息的话,这里就会返回true,整个获得Advisor的过程就成功结束

    在这里插入图片描述

    根据TransactionAttributeSource拿到@Transactional包装的所有事务信息

    这个过程可以是从AbstractFallbackTransactionAttributeSource#getTransactionAttribute开始的,可以跳转查看

    Spring及Spring boot 第四章-第二节 Spring声明式事务管理 AbstractFallbackTransactionAttributeSource

    附录

    解析@Transactional转变过程
    Advisor——Pointcut

    在这里插入图片描述
    在这里插入图片描述

    Pointcut——MethodMatcher

    在这里插入图片描述
    TransactionAttributeSourcePointcut实现的是父类StaticMethodMatcherPointcut的getMethodMatcher
    在这里插入图片描述
    其实就是返回TransactionAttributeSourcePointcut对象自己。
    在这里插入图片描述

    MethodMatcher(Pointcut本身)—— TransactionAttributeSource

    在这里插入图片描述

  • 相关阅读:
    LeetCode刷题(12)
    C语言之程序环境和预处理(2)
    搜索与图论——Prim算法求最小生成树
    ubuntu扩大运行内存, 防止编译卡死
    【C++学习】多态
    利用emotion数据集微调deberta-v3-large大模型的文本分类
    echarts常用功能配置项
    ✔ ★【备战实习(面经+项目+算法)】 10.21学习时间表(总计学习时间:5h30min)(算法刷题:7道)
    VS中cmake多配置构建设置
    新知识经济时代,谁在生产知识?
  • 原文地址:https://blog.csdn.net/qq_44587855/article/details/123294870
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号