aop 通知的執行順序


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();
    }

執行順序:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM