Springboot uses transaction principle

In the springboot project, there is no need to use the @EnableTransactionManagement annotation, you can directly add the @Transactional annotation to the business method, but if it is a pure springmvc project, you need to add the @EnableTransactionManagement annotation to the configuration class to start the global transaction

In fact, the reason is very simple, because the springboot project automatically added the @EnableTransactionManagement annotation to us when it was started;

In the jar package spring-boot-autoconfigure-2.2.2.RELEASE.jar, the beans to be automatically injected are defined

org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

However, there is a prerequisite for the injection of this bean. TransactionAutoConfiguration must be injected after the bean DataSourceTransactionManagerAutoConfiguration is injected.

In the TransactionAutoConfiguration bean, a static inner class is declared: EnableTransactionManagementConfiguration
In its class, a comment to enable transaction annotations is declared

@Configuration(
        proxyBeanMethods = false
)
@ConditionalOnBean({
    
    TransactionManager.class})
@ConditionalOnMissingBean({
    
    AbstractTransactionManagementConfiguration.class})
public static class EnableTransactionManagementConfiguration {
    
    
	public EnableTransactionManagementConfiguration() {
    
    
	}

	@Configuration(
		proxyBeanMethods = false
	)
	@EnableTransactionManagement(
		proxyTargetClass = true
	)
	@ConditionalOnProperty(
		prefix = "spring.aop",
		name = {
    
    "proxy-target-class"},
		havingValue = "true",
		matchIfMissing = true
	)
	public static class CglibAutoProxyConfiguration {
    
    
		public CglibAutoProxyConfiguration() {
    
    
		}
	}

	@Configuration(
		proxyBeanMethods = false
	)
	@EnableTransactionManagement(
		proxyTargetClass = false
	)
	@ConditionalOnProperty(
		prefix = "spring.aop",
		name = {
    
    "proxy-target-class"},
		havingValue = "false",
		matchIfMissing = false
	)
	public static class JdkDynamicAutoProxyConfiguration {
    
    
		public JdkDynamicAutoProxyConfiguration() {
    
    
		}
	}
}

Guess you like

Origin blog.csdn.net/CPLASF_/article/details/112168218