1
luodangjia
2025-01-20 ee44a74a7f3674b1f019c823f3f459a2b08c8bd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ruoyi.common.core.aspect;
 
 
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
 
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 全局事务切面配置
 */
@Aspect
@Configuration
public class SpringTxAspect {
 
    /**
     * 切面,根据自己的项目定义不同的表达式execution
     **/
    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.ruoyi.*.service..*.*(..))";
    @Resource
    private PlatformTransactionManager transactionManager;
 
    /**
     * 增强(事务)的属性的配置
     *
     * @param
     * @return TransactionInterceptor
     * @title: txAdvice
     * @author luofl 2025-1-20
     * @Description: 配置
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @Bean
    public TransactionInterceptor txAdvice() {
        NameMatchTransactionAttributeSource txAttributeS = new NameMatchTransactionAttributeSource();
        RuleBasedTransactionAttribute requiredAttr = new RuleBasedTransactionAttribute();
        // PROPAGATION_REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中
        requiredAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        // 抛出异常后执行切点回滚
        requiredAttr.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        //
        RuleBasedTransactionAttribute supportsAttr = new RuleBasedTransactionAttribute();
        // PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行
        supportsAttr.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
        // 只读事务,不做更新操作
        supportsAttr.setReadOnly(true);
 
        // 注意:方法名称来自类匹配的到方法【save*, “*”表示匹配任意個字符】
        Map txMethod = new HashMap();
        txMethod.put("save*", requiredAttr);
        txMethod.put("insert*", requiredAttr);
        txMethod.put("add*", requiredAttr);
        txMethod.put("update*", requiredAttr);
        txMethod.put("modify*", requiredAttr);
        txMethod.put("remove*", requiredAttr);
        txMethod.put("delete*", requiredAttr);
        txMethod.put("bind*", requiredAttr);
        txMethod.put("unbind*", requiredAttr);
        txMethod.put("create*", requiredAttr);
        txMethod.put("change*", requiredAttr);
        txMethod.put("register*", requiredAttr);
        // readOnly = true
        txMethod.put("select*", supportsAttr);
        txMethod.put("get*", supportsAttr);
        txMethod.put("find*", supportsAttr);
        txMethod.put("query*", supportsAttr);
        txMethod.put("read*", supportsAttr);
        txMethod.put("check*", supportsAttr);
        //
        txAttributeS.setNameMap(txMethod);
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, txAttributeS);
        return txAdvice;
    }
 
    /**
     * AOP配置定义切面和切点的信息
     *
     * @return Advisor
     * @title: txAdviceAdvisor
     * @author luofl 2025-1-20
     * @Description: AdvisorBean
     */
    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
}