Spring Boot 切面配置式 事务

转载:https://gitee.com/eagleFlySky/codes/pects6ro0l4dw3qgbiz8h75

/*

  • Copyright:Copyright©2017-2020
  • Company:ygego
    */
    package cn.enn.ygego.sunny.servicepurchase.config;

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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
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.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**

  • ClassName: DataFormatFilter

  • Description:声明式事务配置

  • Author: zhaojinpeng

  • Date: 2018/3/17 14:53

  • History:

  • <2018/3/17 14:53> <1.0>

  • 作者姓名 修改时间 版本号 描述
    */
    @Aspect
    @Configuration
    public class TxConfig {

    /**

    • 切面 Service
      /
      private static final String AOP_POINTCUT_EXPRESSION = "execution(
      cn.enn.ygego.sunny.servicepurchase.service.Service.(…))";

    @Autowired
    private DataSourceTransactionManager transactionManager;

    /**

    • 事务拦截器

    • @return
      */
      @Bean
      public TransactionInterceptor txAdvice() {
      NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();

      /只读事务,不做更新操作/
      RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
      readOnlyTx.setReadOnly(true);
      readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);

      /当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务/
      RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
      requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
      requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

      Map txMap = new HashMap<>(16);

      /配置事务方法的前缀/
      txMap.put(“add*”, requiredTx);
      txMap.put(“save*”, requiredTx);
      txMap.put(“insert*”, requiredTx);
      txMap.put(“create*”, requiredTx);
      txMap.put(“batch*”, requiredTx);
      txMap.put(“update*”, requiredTx);
      txMap.put(“modify*”, requiredTx);
      txMap.put(“delete*”, requiredTx);
      /配置只读事务方法的前缀/
      txMap.put(“get*”, readOnlyTx);
      txMap.put(“query*”, readOnlyTx);
      txMap.put(“find*”, readOnlyTx);
      txMap.put(“select*”, readOnlyTx);
      /其余方法添加事务/
      txMap.put("*", requiredTx);
      source.setNameMap(txMap);

      return new TransactionInterceptor(transactionManager, source);
      }

    /**

    • 注册事务
    • @param txAdvice
    • @return
      */
      @Bean
      public Advisor txAdviceAdvisor(TransactionInterceptor txAdvice) {
      AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
      pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
      return new DefaultPointcutAdvisor(pointcut, txAdvice);
      }

}

你可能感兴趣的:(Spring)