概述
TransactionAutoConfiguration
是Spring Boot
关于事务的自动配置类。它仅在类PlatformTransactionManager
存在于classpath
上时生效。并且在以下自动配置类应用之后应用 :
JtaAutoConfiguration
HibernateJpaAutoConfiguration
DataSourceTransactionManagerAutoConfiguration
Neo4jDataAutoConfiguration
TransactionAutoConfiguration
定义了如下 bean
:
TransactionManagerCustomizers platformTransactionManagerCustomizers
对容器中存在的所有
PlatformTransactionManagerCustomizer
bean
组件的一个包装TransactionTemplate transactionTemplate
仅在容器中只存在一个
PlatformTransactionManager
bean
组件时才定义
另外TransactionAutoConfiguration
根据配置属性项spring.aop.proxy-target-class
的使用情况决定如何使用注解@EnableTransactionManagement
:
spring.aop.proxy-target-class
明确被设置为false
使用注解
@EnableTransactionManagement(proxyTargetClass = false)
spring.aop.proxy-target-class
未被使用或者被设置为true
使用注解
@EnableTransactionManagement(proxyTargetClass = true)
TransactionAutoConfiguration
使用了注解@EnableTransactionManagement
,而注解@EnableTransactionManagement
进一步引入了类TransactionManagementConfigurationSelector
,这是一个根据@EnableTransactionManagement
注解属性mode
决定导入哪些类的一个ImportSelector
。缺省情况下,注解@EnableTransactionManagement
的属性mode
为PROXY
,这种情况下TransactionManagementConfigurationSelector
相应地导入了AutoProxyRegistrar
和ProxyTransactionManagementConfiguration
。
AutoProxyRegistrar
进而向容器注册了如下基础设置bean
:
bean
org.springframework.aop.config.internalAutoProxyCreator
bean
类 :InfrastructureAdvisorAutoProxyCreator
注册工具
AopConfigUtils#registerAutoProxyCreatorIfNecessary
ProxyTransactionManagementConfiguration
进而向容器注册了如下基础设置bean
:
bean
org.springframework.transaction.config.internalTransactionAdvisor
bean
类 :BeanFactoryTransactionAttributeSourceAdvisor
依赖于
bean
transactionAttributeSource
和transactionInterceptor
bean
transactionInterceptor
bean
类 :TransactionInterceptor
依赖于
bean
transactionAttributeSource
bean
transactionAttributeSource
bean
类 :AnnotationTransactionAttributeSource
bean
org.springframework.transaction.config.internalTransactionalEventListenerFactory
bean
类 :TransactionalEventListenerFactory
由基类
AbstractTransactionManagementConfiguration
定义
源代码
源代码版本 : spring-boot-autoconfigure-2.1.3.RELEASE
package org.springframework.boot.autoconfigure.transaction;
// 省略 imports
@Configuration
// 仅在类 PlatformTransactionManager 存在于 classpath 上时生效
@ConditionalOnClass(PlatformTransactionManager.class)
// 在指定自动配置类应用之后应用
@AutoConfigureAfter({ JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
Neo4jDataAutoConfiguration.class })
// 确保前缀为 spring.transaction 的事务有关属性配置项被加载到 bean TransactionProperties
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {
// 定义 bean TransactionManagerCustomizers platformTransactionManagerCustomizers
@Bean
// 仅在该 bean 不存在时才定义
@ConditionalOnMissingBean
public TransactionManagerCustomizers platformTransactionManagerCustomizers(
ObjectProvider<PlatformTransactionManagerCustomizer<?>> customizers) {
return new TransactionManagerCustomizers(
customizers.orderedStream().collect(Collectors.toList()));
}
// 嵌套配置类
@Configuration
// 仅在只有一个 PlatformTransactionManager bean 时才生效
@ConditionalOnSingleCandidate(PlatformTransactionManager.class)
public static class TransactionTemplateConfiguration {
private final PlatformTransactionManager transactionManager;
public TransactionTemplateConfiguration(
PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
// 定义 bean TransactionTemplate transactionTemplate
@Bean
// 仅在该 bean 不存在时才生效
@ConditionalOnMissingBean
public TransactionTemplate transactionTemplate() {
return new TransactionTemplate(this.transactionManager);
}
}
// 嵌套配置类
@Configuration
// 仅在 bean PlatformTransactionManager 存在时才生效
@ConditionalOnBean(PlatformTransactionManager.class)
// 仅在 bean AbstractTransactionManagementConfiguration 不存在时才生效
@ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
public static class EnableTransactionManagementConfiguration {
// 嵌套配置类
// 在属性 spring.aop.proxy-target-class 被明确设置为 false 时启用注解
// @EnableTransactionManagement(proxyTargetClass = false)
@Configuration
@EnableTransactionManagement(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
matchIfMissing = false)
public static class JdkDynamicAutoProxyConfiguration {
}
// 嵌套配置类
// 在属性 spring.aop.proxy-target-class 缺失或者被明确设置为 true 时启用注解
// @EnableTransactionManagement(proxyTargetClass = true)
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
matchIfMissing = true)
public static class CglibAutoProxyConfiguration {
}
}
}