【Spring】17 AOP 重用切入点定义

定义方法用于切入点表达式

	/**
     *  定义一个方法,用于声明切入点表达式,一般地,该方法中不需要添加其他的代码。
     *  使用 @Pointcut 来声明切入点表达式。
     *  后面的其他通知直接使用方法名来引用当前的切入点表达式
     */
    @Pointcut("execution(public int com.test.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));
    }

同包引用(在方法名前加类名使用)

	@Before("LoggingAspect.declareJoinPointExpression()")
    public void validateArgs(JoinPoint joinPoint){
        System.out.println("--> validate:" + Arrays.asList(joinPoint.getArgs()));
    }

外包引用(在类名前加类路径再加方法名使用)

	@Before("com.test.spring.aop.LoggingAspect.declareJoinPointExpression()")
    public void validateArgs(JoinPoint joinPoint){
        System.out.println("--> validate:" + Arrays.asList(joinPoint.getArgs()));
    }
posted @ 2019-03-21 10:58  ZMemory  阅读(203)  评论(0)    收藏  举报