@Aspect class 带有类似于切入点表达式的通知签名

@Aspect class with an advice signature similar to pointcut expression

我在学习AOP的时候遇到了一个场景。

class 中的所有 都在包 com.spring 中 并且 Pointcut 定义了 @AfterReturning 类型的 Advice 对于包 com.spring 的任何 class 中的任何方法,具有任意数量的参数.

spring.xml is well defined, in my classpath and the provided code is running

所以我的问题是,Aspects class 运行 的这个建议不应该无限地满足 Pointcut 定义吗?

这是我的看点Class

package com.spring;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class Aspects {

    @AfterReturning(pointcut = "execution(* com.spring.*.*(..))", returning= "res")
    public void logAfterExecutionAdvice(int res){
        System.out.print("Result in advice: "+res);
         System.out.print(" In After Advice ");
  }
}

我的加法器Class

package com.spring;

public class Adder {
    private int a;
    private int b;
    public int add(int a,int b){
       return (a+b);
    }
    /**
     * @return the a
     */
    public int getA() {
        return a;
    }
    /**
     * @param a the a to set
     */
    public void setA(int a) {
        this.a = a;
    }
    /**
     * @return the b
     */
    public int getB() {
        return b;
    }
    /**
     * @param b the b to set
     */
    public void setB(int b) {
        this.b = b;
    }
}

还有我的主要Class

package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
    Adder adder=(Adder)ctx.getBean("adder");
    System.out.print("Result=" + adder.add(2,3));
    }

}

我得到的输出是建议结果:5 In After Advice Result=5

根据 Spring 的 AOP 文档 here -

In Spring AOP, it is not possible to have aspects themselves be the target of advice from other aspects. The @Aspect annotation on a class marks it as an aspect, and hence excludes it from auto-proxying.

类 标记为 @Aspect 被排除在自动代理和切入点之外。

所以,如果你尝试这样的事情 -

package com.spring;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class Aspects {
    @After("execution(* com.spring.Aspects.*(..))")
    public void logAfterExecutionAdvice(){
        System.out.print("In After Advice ");
    }
}

Spring 会给出类似于 -

的错误
Error:(12, 0) ajc: advice defined in com.spring.Aspects has not been applied [Xlint:adviceDidNotMatch]