springboot2 集成Hibernate JPA 用 声明式事物

大多数网站上都让用注解来实现事物控制,其实比较麻烦,还容易漏掉,于是开始研究如何实现声明式事物,其实要搞也简单一个类搞定。

@Configuration
@AutoConfigureAfter(E6DynamicDataSourceConfiguration.class)
public class TransacationConfig {

    private ApplicationContext applicationContext;

    public TransacationConfig(ApplicationContext applicationContext)
    {
        this.applicationContext = applicationContext;
    }



    /**
     * 事物拦截器 千万不要声明一个datasourcetransacationmanager 因为jpa带一个了

     */
    public TransactionInterceptor getAdvisor() {
        Properties properties = new Properties();
        properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly");
        properties.setProperty("find*", "PROPAGATION_REQUIRED,-Exception,readOnly");
        properties.setProperty("select*", "PROPAGATION_REQUIRED,-Exception,readOnly");
        properties.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("create*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("del*", "PROPAGATION_REQUIRED,-Exception");
        TransactionInterceptor result = new TransactionInterceptor(null, properties);
        result.setBeanFactory(this.applicationContext);
        return result;
    }

    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.e6yun.project..service.impl.*.*(..)) && !execution (* com.e6yun.project.common.module.base.user.service.impl.*.*(..)) ";


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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值