【事务】Java Hutool Session 中实现 注解开启事务

目录

说明

注解代码

实现AOP代码


【事务】Java Hutool Session 中实现 注解开启事务

 

说明

https://www.hutool.cn/

【事务】Java Hutool Session 中实现 注解开启事务_第1张图片

 

 

注解代码

package com.xxx.base.common.aop;

import java.lang.annotation.*;

/**
 * @author Mr.Guan
 * @Title: 事物
 * @Package ${package_name}
 * @Description: ${todo}
 * @Mail [email protected]
 * @date 2018
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TransactionHutool {


    //增加参数,用来支持不同数据源
    public String dataSourceName() default "default";

}

 

实现AOP代码

package com.xxx.base.common.aop;

import cn.hutool.db.Session;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @program:
 *
 * @description: 事物aop
 *
 * 使用该事物需要用该方法获取Session
 * TransactionHutoolAspect.local.get()
 *
 * @author: Mr.Guan
 *
 * @Mail: [email protected]
 *
 * @create: 2018
 **/
@Component
@Scope
@Aspect
public class TransactionHutoolAspect {


    public static ThreadLocal local = new ThreadLocal();

    /**
     * 日志
     */
    private static final Log LOG = LogFactory.get();

    /**
     * Service层切点  增加事务
     * TODO 该路径应写成TransactionHutool注解的路径
     */
    @Pointcut("@annotation(com.xxx.base.common.aop.TransactionHutool)")
    public void ServiceAspect() {

    }

    @Around("ServiceAspect()")
    public  Object around(ProceedingJoinPoint joinPoint) {
        Object obj = null;

        String dsName = null;
        try{

            Object[] args = joinPoint.getArgs();
            Class[] argTypes = new Class[joinPoint.getArgs().length];
            for (int i = 0; i < args.length; i++) {
                argTypes[i] = args[i].getClass();
            }
            Method method = null;
            try {
                method = joinPoint.getTarget().getClass().getMethod(joinPoint.getSignature().getName(), argTypes);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }

            TransactionHutool ma = method.getAnnotation(TransactionHutool.class);
            dsName = ma.dataSourceName();

        }catch (Exception e) {

        }

        try {
            Session session = Session.create(dsName);
            //关闭自动提交事务
            session.getConnection().setAutoCommit(false);
            local.set(session);
            //开启事物
            session.beginTransaction();
            LOG.debug("开启事务");

            //执行方法
            obj = joinPoint.proceed();


            local.get().commit();
            LOG.debug("提交事务");

        } catch (Throwable e) {
            local.get().quietRollback();
            //回滚事物
            LOG.debug("出错:回滚事务");
            e.printStackTrace();
            //需要将异常抛出去,否则其他AOP捕获不到异常,无法做出处理
            throw e;
        }finally {
            local.get().close();
        }
        return obj;
    }


}

 

你可能感兴趣的:(工具自用,后端开发,java,aop,spring)