基于AOP实现整体项目事务控制

事务在项目中的用法

在项目中,我们事务一般使用注解的方式控制,当我们的实现类多的时候,业务都需要事务控制的时候,每个方法加 注解,会发现代码十分十分冗余

在这里插入图片描述
我们可以利用Spring 生态下的 AOP实现事务的统一控制

下面我们就介绍下基于AOP事务统一控制(以springboot项目为例)

  1. 引入 需要的pom 文件
<!-- aop依赖 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>


		<aspectj.version>1.8.9</aspectj.version>
  1. 创建 TransactionManagerConfig 类
// 使用Aspect 注解与  Configuration
@Aspect
@Configuration
public class TransactionManagerConfig  {
    private final static Logger logger = LoggerFactory.getLogger(TransactionManagerConfig.class);
    private static final int AOP_TIME_OUT = 50000;
    private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.yxl.demo.impl.*Impl.*(..)))";

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean
    public TransactionInterceptor txAdvice(){
        //new NameMatchTransactionAttributeSource 事务属性
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();

        /** 只读事务,不做更新操作 */
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        /** 当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务 */
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();

        /** 什么异常需要回滚 */
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        requiredTx.setTimeout(AOP_TIME_OUT);

        Map<String, TransactionAttribute> methodMap = new HashMap<>();
        // 回滚事务的方法,增删改
        methodMap.put("add*", requiredTx);
        methodMap.put("save*", requiredTx);
        methodMap.put("update*", requiredTx);
        methodMap.put("modify*", requiredTx);
        methodMap.put("edit*", requiredTx);
        methodMap.put("insert*", requiredTx);
        methodMap.put("delete*", requiredTx);
        methodMap.put("remove*", requiredTx);

        /** 其他方法无事务,只读 */
        methodMap.put("*", readOnlyTx);
        source.setNameMap(methodMap);

        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }

    @Bean(name = "txAdviceAdvisor")
    public Advisor txAdviceAdvisor() {
        logger.info("===============================创建txAdviceAdvisor===================================");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());

    }

}

他会扫描 com.yxl.demo.impl.Impl.(…) 包下的所有方法的前缀

这时候我们可以 用 delete方法写个空指针测试下

猜你喜欢

转载自blog.csdn.net/qq_41977838/article/details/106170461
今日推荐