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