• SpringBoot使用AOP技术事务配置文件


     1 import java.util.Collections;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 import org.springframework.aop.Advisor;
     5 import org.springframework.aop.aspectj.AspectJExpressionPointcut;
     6 import org.springframework.aop.support.DefaultPointcutAdvisor;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.context.annotation.Configuration;
    10 import org.springframework.transaction.PlatformTransactionManager;
    11 import org.springframework.transaction.TransactionDefinition;
    12 import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
    13 import org.springframework.transaction.interceptor.RollbackRuleAttribute;
    14 import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
    15 import org.springframework.transaction.interceptor.TransactionAttribute;
    16 import org.springframework.transaction.interceptor.TransactionInterceptor;
    17 
    18 /**
    19  * @author yc
    20  * @data 2018年7月28日
    21  * @description 通过AOP切面设置全局事务,拦截service包下面所有方法
    22  *           AOP术语:通知(Advice)、连接点(Joinpoint)、切入点(Pointcut)、切面(Aspect)、
    23  *           目标(Target)、代理(Proxy)、织入(Weaving)
    24  */
    25 @Configuration
    26 public class TransactionManagerConfig {
    27     private static final int TX_METHOD_TIMEOUT = 5;
    28     /* 定义切点变量:拦截test.spring包下所有类的所有方法,返回值类型任意的方法 */
    29     private static final String AOP_POINTCUT_EXPRESSION = "execution (* test.spring.service..*(..))";
    30     @Autowired
    31     private PlatformTransactionManager transactionManager;
    32 
    33     /**
    34      * @author yc
    35      * @data 2018年7月28日
    36      * @description springBoot事务配置
    37      */
    38     @Bean
    39     public TransactionInterceptor TxAdvice() {
    40         /* 事务管理规则,声明具备事务管理的方法名 */
    41         NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
    42         /* 只读事物、不做更新删除等 */
    43         /* 当前存在事务就用当前的事务,当前不存在事务就创建一个新的事务 */
    44         RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute();
    45         /* 设置当前事务是否为只读事务,true为只读 */
    46         readOnlyRule.setReadOnly(true);
    47         /*
    48          * transactiondefinition 定义事务的隔离级别;
    49          * PROPAGATION_NOT_SUPPORTED事务传播级别5,以非事务运行,如果当前存在事务,则把当前事务挂起
    50          */
    51         readOnlyRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    52 
    53         RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute();
    54         /* 抛出异常后执行切点回滚 */
    55         requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
    56         /* PROPAGATION_REQUIRED:事务隔离性为1,若当前存在事务,则加入该事务;如果当前没有事务,则创建一个新的事务。这是默认值。 */
    57         requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    58         /* 设置事务失效时间,如果超过5秒,则回滚事务 */
    59         requireRule.setTimeout(TX_METHOD_TIMEOUT);
    60         Map<String, TransactionAttribute> txMap = new HashMap<>();
    61 
    62         txMap.put("add*", requireRule);
    63         txMap.put("save*", requireRule);
    64         txMap.put("insert*", requireRule);
    65         txMap.put("update*", requireRule);
    66         txMap.put("delete*", requireRule);
    67         txMap.put("remove*", requireRule);
    68 
    69         txMap.put("get*", readOnlyRule);
    70         txMap.put("query*", readOnlyRule);
    71         txMap.put("find*", readOnlyRule);
    72         txMap.put("select*", readOnlyRule);
    73         source.setNameMap(txMap);
    74         TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
    75         return txAdvice;
    76     }
    77 
    78     /**
    79      * @author yc
    80      * @data 2018年7月27日
    81      * @description 利用AspectJExpressionPointcut设置切面=切点+通知(写成内部bean的方式)
    82      * 
    83      */
    84     @Bean
    85     public Advisor txAdviceAdvisor() {
    86         /*
    87          * 声明切点的面切面(Aspect):切面就是通知和切入点的结合。通知和切入点共同定义了关于切面的全部内容——它的功能、在何时和何地完成其功能。
    88          */
    89         AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
    90         /* 声明和设置需要拦截的方法,用切点语言描写 */
    91         pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
    92         /* 设置切面=切点pointcut+通知TxAdvice */
    93         return new DefaultPointcutAdvisor(pointcut, TxAdvice());
    94     }
    95 }
  • 相关阅读:
    InnoDB实现MVCC原理
    Python中定义函数时参数有默认值的小陷阱
    Python系统编程笔记
    Python中的字典
    Python中常见的字符串小笔试题
    Oracle常见名词解析
    Oracle数据库面试题【转载】
    Oracle日期语言修改
    Oracle日期时间函数大全
    Oracle数据库分页的三种方法
  • 原文地址:https://www.cnblogs.com/yuhuiqing/p/10701692.html
Copyright © 2020-2023  润新知