@EnableTransactionManagement in Spring Boot

后端 未结 6 1785
你的背包
你的背包 2020-12-07 16:58

Is @EnableTransactionManagement required in Spring Boot? I did some research. Some folks say you don\'t need it, as Spring Boot has it already enabled, others s

6条回答
  •  半阙折子戏
    2020-12-07 17:05

    Little old post but the answers given previously were not straight forward when I was searching for it.

    @EnableTransactionManagement is optional in Spring boot, provided that spring-data* or spring-tx are found in classpath. How it works? As below:

    Spring boot adds a spring-boot-autoconfigure.jar in the classpath. Go to the META-INF's spring.factories file and you can see org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration entry there. This initializes the transaction auto configuration for you.

    Note that the class has following lines: (snippet)

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

    Have a look at TransactionAutoConfiguration to see that it enables transaction support if the PlatformTransactionManager is available in classpath. EnableTransactionManagementConfiguration is also configured there.

提交回复
热议问题