Spring Boot中不需要加 @EnableTransactionManagement 来开启事务

1 Spring中 纯XML 配置事务
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    property name="dataSource" ref="pooledDataSource"/
/bean
aop:config
    aop:pointcut expression="execution(* cn.yuanyu.crud.service..*(..))" id="txPoint"/
    aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/
/aop:config
tx:advice id="txAdvice" transaction-manager="transactionManager"
    tx:attributes
        tx:method name="*"/
        tx:method name="get*" read-only="true"/
    /tx:attributes
/tx:advice

2 Spring中 XML+注解 配置事务

一般xml里面配置粗粒度的控制,然后使用注解

bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    property name="dataSource" ref="pooledDataSource"/
/bean
aop:config
    aop:pointcut expression="execution(* cn.yuanyu.crud.service..*(..))" id="txPoint"/
    aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/
/aop:config
tx:advice id="txAdvice" transaction-manager="transactionManager"
    tx:attributes
        tx:method name="*"/
        tx:method name="get*" read-only="true"/
    /tx:attributes
/tx:advice
!--

--
tx:annotation-driven transaction-manager="transactionManager"/
3 Spring中 纯注解 配置事务
@Configuration //声明配置类
@MapperScan("cn.yuanyu.tx.mapper")
@EnableTransactionManagement // 开启事务注解,等同于配置文件tx:annotation-driven/
public class MybatisPlusConfiguration {

4 Spirng Boot 中无需做任何事情

~/org/springframework/boot/spring-boot-autoconfigure/2.3.0.RELEASE/spring-boot-autoconfigure-2.3.0.RELEASE.jar!/META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
/**
 * Auto-configuration for Spring transaction.
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(PlatformTransactionManager.class)
@AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {
    //...
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnBean(TransactionManager.class)
    @ConditionalOnMissingBean(AbstractTransactionManagementConfiguration.class)
    public static class EnableTransactionManagementConfiguration {
        @Configuration(proxyBeanMethods = false)
        @EnableTransactionManagement(proxyTargetClass = false) //这里其实以及加了
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
        public static class JdkDynamicAutoProxyConfiguration {}
        @Configuration(proxyBeanMethods = false)
        @EnableTransactionManagement(proxyTargetClass = true)  //这里其实以及加了
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
        public static class CglibAutoProxyConfiguration {}
    }
}

同理不需要加 @EnableAspectJAutoProxy 来开启aop

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
public class AopAutoConfiguration {
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnClass(Advice.class)
    static class AspectJAutoProxyingConfiguration {
        @Configuration(proxyBeanMethods = false)
        @EnableAspectJAutoProxy(proxyTargetClass = false)
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = false)
        static class JdkDynamicAutoProxyConfiguration {}
        @Configuration(proxyBeanMethods = false)
        @EnableAspectJAutoProxy(proxyTargetClass = true)
        @ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = true)
        static class CglibAutoProxyConfiguration {}
    }
}

 

最新回复(0)
/jishu8XaGgw_2Fvk5ZRD4MknMP7Ejy8ACli7URKOK1TY1RAzPI_3D8