首页 > 编程语言 >springboot事务源码分析

springboot事务源码分析

时间:2022-11-26 21:22:29浏览次数:42  
标签:事务 false springboot 查看 EnableTransactionManagement class 源码 public

1、本次使用springboot框架分析事务源码

2、打开spring-boot-autoconfigure查看spring.factories发现关于事务的自动配置包含:DataSourceTransactionManagerAutoConfiguration、TransactionAutoConfiguration

 

 

3、查看 TransactionAutoConfiguration源码发现包含注解@EnableTransactionManagement

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(PlatformTransactionManager.class)
@AutoConfigureAfter({ JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class })
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {        
    ...
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnBean(TransactionManager.class)
    @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
    public static class EnableTransactionManagementConfiguration {
        @Configuration(proxyBeanMethods = false)
        @EnableTransactionManagement(proxyTargetClass = false)
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
                matchIfMissing = false)
        public static class JdkDynamicAutoProxyConfiguration {
        }
        @Configuration(proxyBeanMethods = false)
        @EnableTransactionManagement(proxyTargetClass = true)
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
                matchIfMissing = true)
        public static class CglibAutoProxyConfiguration {
        }
    }
}

4、查看@EnableTransactionManagement源码,发现TransactionManagementConfigurationSelector(springboot查看某一功能的具体过程,先查看@Enable...开头的源码)

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({TransactionManagementConfigurationSelector.class})
public @interface EnableTransactionManagement {
    boolean proxyTargetClass() default false;

    AdviceMode mode() default AdviceMode.PROXY;

    int order() default 2147483647;
}

5、查看TransactionManagementConfigurationSelector源码,发现其最终实现了ImportSelector(主要是通过selectImports导入class实现选择行bean注入)

public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {protected String[] selectImports(AdviceMode adviceMode) {
        switch(adviceMode) {
        case PROXY:
            return new String[]{AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
        case ASPECTJ:
            return new String[]{this.determineTransactionAspectClass()};
        default:
            return null;
        }
    }
    ... 
}

6、继续查看AutoProxyRegistrar和ProxyTransactionManagementConfiguration的源码

查看AutoProxyRegistrar的registerBeanDefinitions方法,发现通过AopConfigUtils的registerAutoProxyCreatorIfNecessary方法,最终注册到父容器中的内容为

registry.registerBeanDefinition(internalAutoProxyCreator,InfrastructureAdvisorAutoProxyCreator)

 

标签:事务,false,springboot,查看,EnableTransactionManagement,class,源码,public
From: https://www.cnblogs.com/yulongzhang/p/16928327.html

相关文章

  • k8s源码分析6-kubectl功能和对象总结
    kubectl的职责主要的工作是处理用户提交的东西(包括,命令行参数,yaml文件等)然后其会把用户提交的这些东西组织成一个数据结构体然后把其发送给APIServerkubectl的代......
  • 实验三·bdev原理和源码分析
    任务配置bdev运行环境运行hello_bdev程序并分析源码通过bdev接口写入数据并读取Bdev是在物理设备上的进一步抽象,物理层有NVM、NVMeOF、CEPHRBD等,通过bdev向......
  • 实验四·blobstore原理和源码分析
    实验任务学习Blob基本原理完成hello_blob程序运行修改底层bdev为nvmeBlob构建在bdev之上,是对数据存储的进一步抽象,类似于文件,但是不具备文件POSIX接口,可近似按文件形......
  • springboot
    目录单元测试单元测试要用到spring管理的bean那种......
  • easylogging++的那些事(四)源码分析(二)日志记录宏(一)CLOG宏(四)日志信息保存
    目录writer类的输出运算符writer类的流操控符el::base::MessageBuilder类CLOG宏接口调用流程图在上一篇中我们分析完了CLOG宏日志输出的流程,在结尾的时候我们提......
  • 在CentOS编译Git源码
    Git是一个免费的开源分布式版本控制系统,旨在处理从小到小到的所有内容具有速度和效率的超大型项目。Git易于学习,占用空间很小,性能快如闪电。它超越了Subversion,CVS,Per......
  • CentOS7源码安装Nginx1.22
    CentOS7源码安装Nginx1.22一、安装下载源码包:wgethttp://nginx.org/download/nginx-1.22.1.tar.gz安装依赖:yum-yinstallgccmakepcrepcre-developensslope......
  • springboot学习
    package com.cy.pj.common.cache;@Component        @Scope("singleton")@Lazy//不会启动的时候就加载类        public class DefaultCache{......
  • java——JDBC——JDBC事务管理
      ##JDBC控制事务:1.事务:一个包含多个步骤的业务操作。如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败。2.操作:1.开启事务......
  • AFL源码分析(一)
    AFL源码分析(一)文章首发于:ChaMd5公众号​​https://mp.weixin.qq.com/s/E-D_M25xv5gIpRa6k8xOvw​​a.alf-gcc.c1.find_as这个函数的功能是获取使用的汇编器。首先获取环境......