zoukankan      html  css  js  c++  java
  • Spring-AOP(切面的优先级&&&重用切点表达式)

    可以使用@Order 注解指定切面的优先级,值越小优先级越高

    切面一:

    /*
    * 可以使用@Order 注解指定切面的优先级,值越小优先级越高
    * */
    @Order(2)
    @Component
    @Aspect
    public class LoggingAspect {
        /**
         * 在每一个接口的实现类的每一个方法开始之前执行一段代码
         */
    
        @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
    
            System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
        }
    }

    切面二:

    @Order(1)
    @Aspect
    @Component
    public class VlidationAspect {
    
        @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
        public void validateArgs(JoinPoint joinPoint){
            System.out.println("------->validate:" + Arrays.asList(joinPoint.getArgs()));
        }
    }

    切入点表达式的引用:

    切面一:

    ========引用========

    该类中引用:方法名

    @Order(2)
    @Component
    @Aspect
    public class LoggingAspect {
    
        /*
        * 定义一个方法,用于声明切入点表达式。一般地,该方法中不需要添入其他的代码。
        * 使用@Pointcut 来声明切入点表达式。
        * 后面的其他通知直接使用方法名来引用当前的切入点表达式。
        * */
        @Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
        public void declareJoinPointExpression(){
    
        }
    
    
        /**
         * 在每一个接口的实现类的每一个方法开始之前执行一段代码
         */
    
        @Before("declareJoinPointExpression()")
        public void beforeMethod(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
    
            System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));
        }
    
    }

    切面二:

    ========引用========

    同一包下的:类名.方法名

    不同包下的:包名.类名.方法名

    @Order(1)
    @Aspect
    @Component
    public class VlidationAspect {
    
        @Before("com.atguigu.spring.aop.LoggingAspect.declareJoinPointExpression()")
        public void validateArgs(JoinPoint joinPoint){
            System.out.println("------->validate:" + Arrays.asList(joinPoint.getArgs()));
        }
    }
  • 相关阅读:
    线程应用:(九)阻塞队列的应用
    线程应用:(八)Semaphere信号灯、CyclicBarrier汇合、CountDownLatch计数器、Exchanger
    线程应用:(七)Lock & Condition同步通信
    线程应用:(六)线程池、Callable与Future
    线程应用:(五)传统线程使用实例
    记一次linux安装mysql
    Pycharm断点调试入门
    django admin后台插件:django-suit入门
    在django中使用django_debug_toolbar
    pyquery详细用法
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/12025282.html
Copyright © 2011-2022 走看看