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()));
        }
    }
  • 相关阅读:
    敢为技术定坤乾
    蒙古包作客有感
    少时儿伴他乡遇
    花漫锦宫城
    游红原大草原有感
    把日子过成一首诗
    Request.UrlReferrer详解
    INFO: HTTP GET and HTTP POST Are Disabled by Default
    请求因 HTTP 状态 401 失败:Access Denied。
    SoapRpcMethod.OneWay相关
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/12025282.html
Copyright © 2011-2022 走看看