zoukankan      html  css  js  c++  java
  • 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();
        }

    执行顺序:

  • 相关阅读:
    Java 7 中 NIO.2 的使用——第二节 元数据文件的属性
    Java 7 中 NIO.2 的使用——第一节 Path 类的使用
    使用第三方工具覆写Object中方法
    Java Synchronized Blocks vs. Methods
    生如夏花,死如秋叶
    Struts2中的ModelDriven机制及其运用(转)
    Java 调用 Javascript 函数的范例
    枚举实现工厂模式
    使用Java 8 Lambda表达式对Employee类进行操作
    自定义异常时如何定义checked异常和unchecked异常
  • 原文地址:https://www.cnblogs.com/lioa/p/11811781.html
Copyright © 2011-2022 走看看