栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 自定义事务切面

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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;

    
    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
    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);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号