springboot 全局事物

1. pom 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. TransactionAdviceConfig 全局事物类

(* *.service.*.*(..))中几个通配符的含义: 

1、execution(): 表达式主体。

 2、第一个*号:表示返回类型,*号表示所有的类型。

 3.  com.spf.boot.service.impl 表示匹配所有的service包路径

 4、第二个*号:表示类名,*号表示所有的类。

 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

package com.spf.boot.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.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Properties;


@Aspect
@Configuration
public class TransactionAdviceConfig {

    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.spf.boot.service.impl.*.*(..))";

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean
    public TransactionInterceptor txAdvice() {
        TransactionInterceptor tsi = new TransactionInterceptor();
        Properties properties = new Properties();
        properties.setProperty("get*", "PROPAGATION_SUPPORTS");
        properties.setProperty("select*", "PROPAGATION_SUPPORTS");
        properties.setProperty("load*", "PROPAGATION_SUPPORTS");
        properties.setProperty("query*", "PROPAGATION_SUPPORTS");
        properties.setProperty("list*", "PROPAGATION_SUPPORTS");
        properties.setProperty("add*", "PROPAGATION_REQUIRED");
        properties.setProperty("insert*", "PROPAGATION_REQUIRED");
        properties.setProperty("save*", "PROPAGATION_REQUIRED");
        properties.setProperty("update*", "PROPAGATION_REQUIRED");
        properties.setProperty("modify*", "PROPAGATION_REQUIRED");
        properties.setProperty("do*", "PROPAGATION_REQUIRED");
        properties.setProperty("del*", "PROPAGATION_REQUIRED");
        properties.setProperty("remove*", "PROPAGATION_REQUIRED");
        properties.setProperty("process*", "PROPAGATION_REQUIRED");
        properties.setProperty("create*", "PROPAGATION_REQUIRED");
        properties.setProperty("valid*", "PROPAGATION_REQUIRED");
        properties.setProperty("do*", "PROPAGATION_REQUIRED");
        properties.setProperty("write*", "PROPAGATION_REQUIRED");
        properties.setProperty("cancel*", "PROPAGATION_REQUIRED");
        properties.setProperty("*", "readOnly");
        tsi.setTransactionAttributes(properties);
        tsi.setTransactionManager(transactionManager);
        return tsi;
    }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值