aop与spring事务的异常机制

今天在做自己的项目的时候,明明在配置文件中配置了事务管理机制,然后想测试一下事务是否生效,一直不生效

后来发现原来是因为我自己定义的日志切面把异常抓取了,但是没有抛出去

后来就把异常抛出去就行了,最重要的是

切面里面抛出的异常必须是spring注解抓取的子类或者等于它,具体的看下代码

@Component
@Aspect
public class LoggingAop{

    private static Logger LOGGER = LoggerFactory.getLogger(LoggingAop.class);

    //拦截所有被该注解修饰的所有类下面的所有方法
    @Around("@within(com.zxc.movie.aop.annotation.Log)")
    public Object before(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        Transactional transactional = null ;
        MethodSignature methodSignature = null;
        Class<?> clazz = null;
       try{
           clazz = proceedingJoinPoint.getTarget().getClass();
           //就是为了获取当前执行的方法,可以忽略不看
           methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();;
           Method method =clazz.getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
           LogIgnore logIgnore = method.getAnnotation(LogIgnore.class);
           transactional = method.getAnnotation(Transactional.class);
           //前置日志
           if(logIgnore == null || logIgnore != null && logIgnore.before()) {
               LOGGER.info(clazz.getName() + " 执行了 " +  methodSignature.getName() + " 方法,参数为{}", Arrays.asList(proceedingJoinPoint.getArgs()));
           }
           Object object = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
           //后置日志
           if(logIgnore == null || logIgnore != null && logIgnore.after()) {
               LOGGER.info(clazz.getName() + " 成功执行了 " +  methodSignature.getName() + " 方法,返回值为{}", object);
           }
       }catch (Throwable throwable) {
           //异常日志 TODO 后期需要通知对应人员进行一个处理
           LOGGER.error(clazz.getName() + " 执行了 " +  methodSignature.getName() + " 方法出错,当时传入的参数为{}", Arrays.asList(proceedingJoinPoint.getArgs()));
           /**
            * 如果用了事务,那么异常要抛回给spring自身去处理
            * 重要!!! -- 只能抛出spring注解处理的异常的子类或本类,不能抛出比它大的异常,否则spring就拿不到了
            */
           if(transactional != null) {
               throw throwable.getClass().newInstance();
           }
       }
        return false;
    }

 
}

因为我做这个切面的时候本身也是用的注解,所以便同时可以拿到spring上面的注解,如果有那个注解

把此时的异常抛出去给spring注解即可

如果抛出的异常大于spring的异常,那事务将不会被回滚,切记!!

猜你喜欢

转载自blog.csdn.net/zxc_user/article/details/81140030