SpringBoot之统一事务管理配置

SpringBoot之自定义Jackson反序列化日期类型转换配置类

统一事务管理配置

1. SpringBoot版本

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
  </parent>
<dependencies>
 <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
     <version>1.9.7</version>
        </dependency>
    </dependencies>

2. 统一事务管理配置类

package com.yuan.webframework.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;

/**
 * <p>
 * Description: 统一事务管理配置
 * </p>
 *
 * @author jinshengyuan
 * @since 2022/8/5 16:35
 */
@Aspect
@Configuration
public class TransactionManagerConfig {
    //切点表达式
    private static final String AOP_POINT_EXPRESSION = "execution(* com.zx.*.**.service.impl.*.*(..))  or execution(* org.snaker.engine..*.*(..))";

    @Autowired
    PlatformTransactionManager transactionManager;//注入平台(Mybatis)事务管理器

    @Bean
    public TransactionInterceptor txAdvice(){
        //增删改
        DefaultTransactionAttribute txRequired = new DefaultTransactionAttribute();
        txRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txRequired.rollbackOn(new Throwable());
        txRequired.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);

        //除了指定前缀开头的以外,其他方法也支持事务
        DefaultTransactionAttribute txRequiredAll = new DefaultTransactionAttribute();
        txRequiredAll.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
        txRequiredAll.rollbackOn(new Throwable());
        txRequiredAll.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);

        //查
        DefaultTransactionAttribute txRequiredReadOnly = new DefaultTransactionAttribute();
        txRequiredReadOnly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txRequiredReadOnly.setReadOnly(true);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        //切入点切入以下的方法为事务方法
        source.addTransactionalMethod("add*",txRequired);
        source.addTransactionalMethod("save*",txRequired);
        source.addTransactionalMethod("insert*",txRequired);
        source.addTransactionalMethod("update*",txRequired);
        source.addTransactionalMethod("modify*",txRequired);
        source.addTransactionalMethod("delete*",txRequired);
        source.addTransactionalMethod("change*",txRequired);
        source.addTransactionalMethod("move*",txRequired);
        source.addTransactionalMethod("remove*",txRequired);
        source.addTransactionalMethod("submit*",txRequired);
        source.addTransactionalMethod("distribute*",txRequired);
        source.addTransactionalMethod("cancel*",txRequired);
        source.addTransactionalMethod("batch*",txRequired);
        source.addTransactionalMethod("sync*",txRequired);
        source.addTransactionalMethod("set*",txRequired);
        source.addTransactionalMethod("*",txRequiredAll);

        //切入点切入的以下的方法为只读事务方法
        source.addTransactionalMethod("get*",txRequiredReadOnly);
        source.addTransactionalMethod("query*",txRequiredReadOnly);
        source.addTransactionalMethod("select*",txRequiredReadOnly);
        source.addTransactionalMethod("count*",txRequiredReadOnly);
        source.addTransactionalMethod("find*",txRequiredReadOnly);
        source.addTransactionalMethod("search*",txRequiredReadOnly);
        source.addTransactionalMethod("is*",txRequiredReadOnly);

        return  new TransactionInterceptor(transactionManager,source);
    }

    //事务切入点
    @Bean
    public Advisor txAdviceAdvisor(){
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut,txAdvice());
    }

}

3. 主启动类加入开启事务的注解

如主启动类中加入@EnableTransactionManagement注解,如下

package com.yuan;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) throws UnknownHostException {
        SpringApplication.run(MyApplication.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值