首页 > 其他 > 详细

aop 通知的执行顺序

时间:2019-11-07 15:41:47      阅读:139      评论:0      收藏:0      [点我收藏+]
private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(LoggerAop.class);

    /**
     * 线程池 异步记录日志
     */
    private static ExecutorService logExecutorService =  Executors.newCachedThreadPool();

    // 拦截api接口下面的所有类所有方法
    @Pointcut("execution(* com.l.gis.api..*(..))")
    public void logcut() {
    }

    /**
     * @throws Throwable
     */
    @Before(value = "logcut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        String ip = HttpUtil.getIpAddress();
        System.out.println("aop before 前置 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());
    }
    @AfterReturning(value = "logcut()")
    public void afterReturning(JoinPoint joinPoint) throws Throwable {
        System.out.println("aop afterReturning 返回 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());
    }

    @AfterThrowing(value = "logcut()", throwing = "throwable")
    public void afterThrowing(JoinPoint joinPoint , Throwable throwable) throws Throwable {
        System.out.println("aop afterThrowing 异常 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());
    }
    @After(value = "logcut()")
    public void after(JoinPoint joinPoint) throws Throwable {
        System.out.println("aop after 最后 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());
    }
    @Around(value = "logcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("aop around 环绕 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());

        logExecutorService.execute(() -> {
            try {
                System.out.println("aop around 线程池 :" + Thread.currentThread().getId() + " = " + Thread.currentThread().getName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return proceedingJoinPoint.proceed();
    }

执行顺序:

 

技术分享图片

aop 通知的执行顺序

原文:https://www.cnblogs.com/lioa/p/11811781.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!