1. 程式人生 > >SpringBoot原始碼之事物篇(一)事物是在哪裡開啟的呢?

SpringBoot原始碼之事物篇(一)事物是在哪裡開啟的呢?

最近正在學習使用JPA,JPA是預設開啟事物管理的,在哪裡開啟的呢???突然覺得對spring處理事物的原理一竊不通,才有了這次的原始碼閱讀之行,有不正確的地方歡迎大家指正。

先簡略過一下springboot的執行原理

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry)方法

上圖中的imports包括如下內容:

[org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration, 
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration, 
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration, 
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration, 
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration, 
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration, 
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration, 
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration, 
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, 
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, 
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, 
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration, 
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration, 
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, 
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration, 
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration, 
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, 
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, 
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration, 
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration, 
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration, 
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration, 
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration]

看到一個事物相關的自動配置, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,原始碼中有這個註解 @EnableTransactionManagement,事物開啟!!!!:

@Configuration
@ConditionalOnClass(PlatformTransactionManager.class)
@AutoConfigureAfter({ JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        Neo4jDataAutoConfiguration.class })
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public TransactionManagerCustomizers platformTransactionManagerCustomizers(
            ObjectProvider<List<PlatformTransactionManagerCustomizer<?>>> customizers) {
        return new TransactionManagerCustomizers(customizers.getIfAvailable());
    }

    @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);
        }

    }

    @Configuration
    @ConditionalOnBean(PlatformTransactionManager.class)
    @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
    public static class EnableTransactionManagementConfiguration {

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

        }

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

        }

    }

}