带注释的类的所有公共方法的Spring AOP切入点(包括父类方法)

问题描述 投票:0回答:1

我有两堂课

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

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

使用Spring AOP我需要包装:

  • 如果将注释放在类级别上,则所有调用用@MyAnnotation注释的所有公共方法
  • 如果注释在方法级别上,则所有用@MyAnnotation注释的方法。

这是我的切入点


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

这适用于ChildTestClass的公共方法,但是当我调用ParentTestClass#publicMethodOfParent时如何不包装childTestClass.publicMethodOfParent()?如何包含父方法?

java aop spring-aop pointcut
1个回答
0
投票

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

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

来自documentation

inwith:限制匹配以匹配某些类型内的连接点(使用时在匹配类型内声明的方法的执行Spring AOP)。

© www.soinside.com 2019 - 2024. All rights reserved.