springBoot开启事务

问题:springboot的EnableTransactionManagement这注解是不是多余的?

之前看到网上说springBoot项目要用事务,需要在启动类加上@EnableTransactionManagement。
但是我发现我的项目都没加,一样可以用@Transactional注解来控制事务,所以就研究了下。

若按网上所说的就需要做下面两步:

①在启动类加上@EnableTransactionManagement
package com.zcw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableTransactionManagement
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

②在访问数据库的Service方法上加注解
@Transactional(rollbackFor = Exception.class)
public void registerUser(User user) throws Exception {
   ...
   userMapper.updateUser(user);
   ...
}

其实启动类不加上@EnableTransactionManagement,也是可以用@Transactional注解的,因为:@SpringBootApplication的自动配置已经配置好了。

~/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,\
/**
 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
 * Auto-configuration} for Spring transaction.
 *
 * @author Stephane Nicoll
 * @since 1.3.0
 */
@Configuration
@ConditionalOnClass(PlatformTransactionManager.class)
@AutoConfigureAfter({ JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
		DataSourceTransactionManagerAutoConfiguration.class,
		Neo4jDataAutoConfiguration.class })
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {

	......

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

		}

	}

}

附:@Transactional参数说明: 


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>
<!-- 定义切入点,expression为切人点表达式,如下是指定service包下的所有方法,具体以自身实际要求自定义  -->
<aop:config>
    <aop:pointcut expression="execution(* cn.yuanyu.crud.service..*(..))" id="txPoint"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- <tx:advice>定义事务通知,用于指定事务属性,其中“transaction-manager”属性指定事务管理器,并通过<tx:attributes>指定具体需要拦截的方法-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--<tx:method>拦截方法,其中参数有:
            name:方法名称,将匹配的方法注入事务管理,可用通配符
            propagation:事务传播行为,
            isolation:事务隔离级别定义;默认为“DEFAULT”
            timeout:事务超时时间设置,单位为秒,默认-1,表示事务超时将依赖于底层事务系统;
            read-only:事务只读设置,默认为false,表示不是只读;
            rollback-for:需要触发回滚的异常定义,可定义多个,以“,”分割,默认RuntimeException都将导致事务回滚;
            no-rollback-for:不被触发进行回滚的 Exception(s);可定义多个,以“,”分割;
          -->
        <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 {

@Transactional()
public void registerUser(User user) throws Exception {
   ...
}

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值