(⼆⼗九)、SpringBoot配置全局事务(⼆⼗九)、 SpringBoot 配置全局事务
1、简介
前⾯我们讲了。但是每个⽅法都添加注解还是有点⿇烦,下⾯我们看看如何配置全局事务管理器。
2、引⼊Aop 依赖
<!-- aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
3、配置Aop切⾯
package com.fig;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import org.springframework.stereotype.Component;
import ansaction.PlatformTransactionManager;
import ansaction.TransactionDefinition;
import ansaction.TransactionManager;
import ansaction.interceptor.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @Author dw
* @ClassName TransactionManagerConfig
spring framework版本
* @Description 配置全局事务
* @Date 2021/9/25 15:05
* @Version 1.0
*/
@Aspect
@Component
public class TransactionManagerConfig {
private final static Logger logger = Logger(TransactionManagerConfig.class); /**
* 配置⽅法过期时间,默认-1,永不超时,单位毫秒
*/
private static final int AOP_TIME_OUT = 50000;
/**
* Aop 切⾯切⼊点表达式
*/
private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.dw.study.service.impl.*.*(..)))";
@Autowired
//    private PlatformTransactionManager transactionManager;
private TransactionManager transactionManager;
@Bean
public TransactionInterceptor txAdvice(){
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
/** 查询⽅法,只读事务,不做更新操作 */
RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
readOnlyTx.setReadOnly(true);
readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
/** 增、删、改需要的事务 */
RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
/** 什么异常需要回滚 */
requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
/** 当前存在事务就使⽤当前事务,当前不存在事务就创建⼀个新的事务 */
requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
requiredTx.setTimeout(AOP_TIME_OUT);
Map<String, TransactionAttribute> methodMap = new HashMap<>();
/** 可以提及事务或回滚事务的⽅法 */
methodMap.put("add*", requiredTx);
methodMap.put("save*", requiredTx);
methodMap.put("creat*", requiredTx);
methodMap.put("update*", requiredTx);
methodMap.put("modify*", requiredTx);
methodMap.put("edit*", requiredTx);
methodMap.put("insert*", requiredTx);
methodMap.put("delete*", requiredTx);
methodMap.put("remove*", requiredTx);
methodMap.put("repair*", requiredTx);
methodMap.put("binding*", requiredTx);
/** 其他⽅法⽆事务,只读 */
methodMap.put("*", readOnlyTx);
source.setNameMap(methodMap);
TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
return txAdvice;
}
@Bean(name = "txAdviceAdvisor")
public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {
logger.info("===============================创建txAdviceAdvisor===================================");        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
return new DefaultPointcutAdvisor(pointcut, txAdvice);
}
}
注意:对于修改操作,按照上⾯的命名规范命名⽅法,否则都是只读事务,不可做修改操作。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。