Spring 注释 class 的所有 public 方法的 AOP 切入点(包括父 class 方法)

Spring AOP pointcut for all public methods of an annotatted class (including parent class methods)

我有两个classes

public class ParentTestClass {
    public void publicMethodOfParent() {
    }
}

@Component
@MyAnnotation
public class ChildTestClass extends ParentTestClass {
    public void publicMethodOfChild() {
    }
}

使用 Spring AOP 我需要包装:

这是我的切入点


@Around("(@within(MyAnnotation) && execution(public * *(..))) || @annotation(MyAnnotation)")
public Object myWrapper(ProceedingJoinPoint invocation) throws Throwable {
   // ...
}

这适用于 ChildTestClass 的 public 方法,但是当我调用时 ParentTestClass#publicMethodOfParent 没有换行 childTestClass.publicMethodOfParent() 如何包含父方法?

以下切入点表达式也将拦截父方法

来自documentation

@Pointcut("within(com.app..*) && execution(public * com.app..*.*(..))")
public void publicMethodsInApp() {
}

@Around("(publicMethodsInApp() && @target(MyAnnotation)) || "
        + "(publicMethodsInApp() && @annotation(MyAnnotation))")
public Object myWrapper(ProceedingJoinPoint invocation) throws Throwable {
 //..
}

@target: Limits matching to join points (the execution of methods when using Spring AOP) where the class of the executing object has an annotation of the given type.