springboot 自定义事务切面


## ***第一次方式:***

@Aspect
@Configuration
public class TransactionAdviceConfig {
    private static final int TX_METHOD_TIME_OUT = 120;
    private static final String POITCUT_EXPRESSION = "execution (* com..service.*.*(..))";
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    public TransactionAdviceConfig() {
    }

    @Bean
    public TransactionInterceptor txAdvice() {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        Map<String, TransactionAttribute> nameMap = new HashMap(16);
        RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute();
        readOnlyRule.setReadOnly(true);
        readOnlyRule.setPropagationBehavior(0);
        RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute();
        requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requireRule.setPropagationBehavior(0);
        requireRule.setTimeout(120);
        nameMap.put("add*", requireRule);
        nameMap.put("save*", requireRule);
        nameMap.put("insert*", requireRule);
        nameMap.put("update*", requireRule);
        nameMap.put("delete*", requireRule);
        nameMap.put("create*", requireRule);
        nameMap.put("active*", requireRule);
        nameMap.put("approve*", requireRule);
        nameMap.put("remove*", requireRule);
        nameMap.put("invoke*", requireRule);
        nameMap.put("on*", requireRule);
        nameMap.put("handle*", requireRule);
        nameMap.put("restore*", requireRule);
        nameMap.put("modify*", requireRule);
        nameMap.put("charge*", requireRule);
        nameMap.put("refund*", requireRule);
        nameMap.put("payment*", requireRule);
        nameMap.put("batch*", requireRule);
        nameMap.put("get*", readOnlyRule);
        nameMap.put("query*", readOnlyRule);
        nameMap.put("*Query", readOnlyRule);
        nameMap.put("find*", readOnlyRule);
        nameMap.put("select*", readOnlyRule);
        nameMap.put("count*", readOnlyRule);
        nameMap.put("load*", readOnlyRule);
        source.setNameMap(nameMap);
        return new TransactionInterceptor(this.platformTransactionManager, source);
    }

    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution (* com..service.*.*(..))");
        return new DefaultPointcutAdvisor(pointcut, this.txAdvice());
    }
}

## ***第二种方式:***


@Configuration
public class TransactionConfiguration {
                                                            
    public static final String AOP_POINTCUT_EXPRESSION  = "execution(* com.github.modules..service.impl.*(..))";
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    /**
     * 获取事务配置
     * 
     * @return
     */
    public static Properties getAttrubites() {
        Properties attributes = new Properties();
        
        //查询
        attributes.setProperty("get*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        attributes.setProperty("query*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        attributes.setProperty("find*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        attributes.setProperty("select*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
        
        //添加
        attributes.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
        attributes.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
        attributes.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
        attributes.setProperty("create*", "PROPAGATION_REQUIRED,-Exception");

        //更新
        attributes.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        attributes.setProperty("modify*", "PROPAGATION_REQUIRED,-Exception");

        //删除
        attributes.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
        attributes.setProperty("remove*", "PROPAGATION_REQUIRED,-Exception");
        return attributes;

    }

    /**
     * 采用注解实例化的拦截器Bean,注入切面配置信息
     */
    @Bean
    public DefaultPointcutAdvisor  defaultPointcutAdvisor(TransactionInterceptor ti){
        ti.setTransactionManager(platformTransactionManager);
        ti.setTransactionAttributes(getAttrubites());
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, ti);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值