美文网首页
springBoot AOP声明式事务

springBoot AOP声明式事务

作者: 炒面Z | 来源:发表于2018-12-28 10:13 被阅读0次
package *.*.*.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.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;

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

/**
 * AOP事务配置
 */
@Aspect
@Configuration
public class TxAdviceConfig {
    private static final int TX_METHOD_TIMEOUT = 5000;

    private static final String AOP_POINTCUT_EXPRESSION =
        "(execution (* edu.sairobo.elephant.modules.*.service..*.*(..)))" +
            " || (execution (* edu.sairobo.manager.*.service..*.*(..)))";

//    @Autowired
//    private PlatformTransactionManager transactionManager;

    @Autowired
    private DataSourceTransactionManager transactionManager;

    @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);
        requiredTx.setTimeout(TX_METHOD_TIMEOUT);
        Map<String, TransactionAttribute> txMap = new HashMap<>();

        txMap.put("save*", requiredTx);
        txMap.put("remove*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("batch*", requiredTx);
        txMap.put("clear*", requiredTx);
        txMap.put("add*", requiredTx);
        txMap.put("append*", requiredTx);
        txMap.put("modify*", requiredTx);
        txMap.put("edit*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("do*", requiredTx);
        txMap.put("create*", requiredTx);
        /*select,count开头的方法,开启只读,提高数据库访问性能*/
        txMap.put("select*", readOnlyTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("valid*", readOnlyTx);
        txMap.put("list*", readOnlyTx);
        txMap.put("count*", readOnlyTx);
        txMap.put("find*", readOnlyTx);
        txMap.put("load*", readOnlyTx);
        txMap.put("search*", readOnlyTx);
        txMap.put("*", requiredTx);

        source.setNameMap(txMap);
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
}

相关文章

  • springBoot AOP声明式事务

  • spring事务(二) 声明式事务

    spring事务(二) 声明式事务 知识导读 声明式事务是对编程式事务的包装 声明式事务通过使用AOP来实现,注册...

  • Spring 事务之声明式事务

    一.声明式事务实现 将编程式事务章节中applicationContext.xml修改下: 声明式事务通过AOP代...

  • Spring 声明式事务

    Spring事务与AOP的联系   Spring中声明式事务是借助于AOP来实现的,在AOP中我们可以对切面进行配...

  • Java 一些配置

    1.事务的实现方式 (1) 实现方式共有两种:编码方式;声明式事务管理方式。(2) 基于AOP技术实现的声明式事务...

  • spring事务管理

    spring事务管理 使用方式分类:声明式事务 @Transactional 通过AOP来实现的;起到事务管理...

  • spring中的事务管理

    Spring对事务的解决办法其实分为2种:编程式实现事务,AOP配置声明式解决方案。 声明式事物 1)Spring...

  • Spring/Transaction

    事务管理方式spring支持编程式事务管理和声明式事务管理两种方式。声明式事务管理建立在AOP之上的。其本质是对方...

  • 内部调用引起Spring声明式事务@Transactional失

    失效的原因 Spring声明式事务是基于AOP生成的代理类来实现的,而AOP无法拦截内部调用,导致事务失效。 解决...

  • 7.5 Spring事务控制

    在Spring学习的书中,AOP最常见的应用场景就是事务管理了。基于AOP的事务管理是声明式事务,原理就是在方法的...

网友评论

      本文标题:springBoot AOP声明式事务

      本文链接:https://www.haomeiwen.com/subject/pofslqtx.html