Spring Boot 自动配置 : TransactionAutoConfiguration

概述

TransactionAutoConfigurationSpring 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的属性modePROXY,这种情况下TransactionManagementConfigurationSelector相应地导入了AutoProxyRegistrarProxyTransactionManagementConfiguration

AutoProxyRegistrar进而向容器注册了如下基础设置bean :

  • bean org.springframework.aop.config.internalAutoProxyCreator
    • bean类 : InfrastructureAdvisorAutoProxyCreator

    注册工具 AopConfigUtils#registerAutoProxyCreatorIfNecessary

ProxyTransactionManagementConfiguration进而向容器注册了如下基础设置bean:

  • bean org.springframework.transaction.config.internalTransactionAdvisor
    • bean类 : BeanFactoryTransactionAttributeSourceAdvisor

    依赖于bean transactionAttributeSourcetransactionInterceptor

  • 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 {

		}

	}

}

你可能感兴趣的:(Spring,Transaction,Spring,Boot,Spring,Boot,自动配置)