这不是 Spring 或 AspectJ 问题。在 Java 中,接口上的注解、其他注解或方法永远不会通过实现类、使用注解注解的类或覆盖方法来继承。注解继承仅适用于从类到子类,但前提是超类中使用的注解类型带有元注解@Inherited。
更新:因为我之前已经多次回答过这个问题,所以我刚刚在Emulate annotation inheritance for interfaces and methods with AspectJ 中记录了这个问题以及解决方法。
这是一个小证据,证明你想要的东西不起作用,因此也不能被某个方面利用:
package de.scrum_master.app;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface OuterAnnotation {}
package de.scrum_master.app;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@OuterAnnotation
public @interface InnerAnnotation {}
package de.scrum_master.app;
import java.lang.annotation.Annotation;
@InnerAnnotation
public class Application {
public static void main(String[] args) {
for (Annotation annotation : Application.class.getAnnotations())
System.out.println(annotation);
}
}
控制台输出显示 JVM 只看到内部注解,而不是用于内部注解的外部注解:
@de.scrum_master.app.InnerAnnotation()
更新: 对 Bradley M Handy 的回答很感兴趣,我重新检查了它是否也适用于我的代码中描述的情况,确实如此。这种类型的 AspectJ 语法对我来说是未知的,尽管我认为我对 AspectJ 了解很多。所以谢谢,布拉德利。 :-) 这方面会起作用:
package de.scrum_master.aspect;
import de.scrum_master.app.OuterAnnotation;
public aspect MetaAnnotationAspect {
after() : within(@(@OuterAnnotation *) *) && execution(* *(..)) {
System.out.println(thisJoinPoint);
}
}
运行应用程序时的控制台日志:
@de.scrum_master.app.InnerAnnotation()
execution(void de.scrum_master.app.Application.main(String[]))