@Configuration //和DataSourceTransactionManagerAutoConfiguration中是一样的 //引入了spring-boot-starter-jdbc,那自然是存在了PlatformTransactionManager @ConditionalOnClass({PlatformTransactionManager.class}) //这个自动配置类必须要在DataSourceTransactionManagerAutoConfiguration这个自动配置类之后才能生效 //也就是前面我们已经往Spring容器中注入了DataSourceTransactionManager这个对象才执行这个配置类 @AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class}) @EnableConfigurationProperties({TransactionProperties.class}) public class TransactionAutoConfiguration { public TransactionAutoConfiguration() { }
@Configuration @ConditionalOnBean({PlatformTransactionManager.class}) @ConditionalOnMissingBean({AbstractTransactionManagementConfiguration.class}) public static class EnableTransactionManagementConfiguration { public EnableTransactionManagementConfiguration() { }
@Configuration //重点:通过 @EnableTransactionManagement注解开启事务 //可以看到和我们自己使用@EnableTransactionManagement是一样的 @EnableTransactionManagement( proxyTargetClass = true ) @ConditionalOnProperty( prefix = "spring.aop", name = {"proxy-target-class"}, havingValue = "true", matchIfMissing = true ) public static class CglibAutoProxyConfiguration { public CglibAutoProxyConfiguration() { } }
@Configuration @EnableTransactionManagement( proxyTargetClass = false ) @ConditionalOnProperty( prefix = "spring.aop", name = {"proxy-target-class"}, havingValue = "false", matchIfMissing = false ) public static class JdkDynamicAutoProxyConfiguration { public JdkDynamicAutoProxyConfiguration() { } } }
@Configuration @ConditionalOnSingleCandidate(PlatformTransactionManager.class) public static class TransactionTemplateConfiguration { private final PlatformTransactionManager transactionManager;
public TransactionTemplateConfiguration(PlatformTransactionManager transactionManager) { this.transactionManager = transactionManager; }
@Bean @ConditionalOnMissingBean public TransactionTemplate transactionTemplate() { return new TransactionTemplate(this.transactionManager); } } }