这里的问题不是AspectJ,而是JVM。在Java中,
接口,
方法或
其他注释
永远不会被
实现类
覆盖方法或
使用带注释的注释的类。
注释继承仅在类到子类之间起作用,但仅当超类中使用的注释类型带有元注释时@Inherited,请参见JDK JavaDoc。
AspectJ是一种JVM语言,因此可以在JVM的限制内工作。对于此问题,没有通用的解决方案,但是对于要为其模拟注释继承的特定接口或方法,可以使用如下解决方法:
package de.scrum_master.aspect;import de.scrum_master.app.Marker;import de.scrum_master.app.MyInterface;public aspect MarkerAnnotationInheritor { // Implementing classes should inherit marker annotation declare @type: MyInterface+ : @Marker; // Overriding methods 'two' should inherit marker annotation declare @method : void MyInterface+.two() : @Marker;}请注意:有了这一方面,您可以从界面和带注释的方法中删除(文字)注释,因为AspectJ的ITD(类型间定义)机制将它们添加回接口以及所有实现/重写的类/方法。
现在,在运行Applicationsays的控制台日志中:
execution(de.scrum_master.app.Application())execution(void de.scrum_master.app.Application.two())
顺便说一句,您也可以将方面直接嵌入到界面中,以便将所有内容都放在一个位置。请小心重命名MyInterface.java为MyInterface.aj,以帮助AspectJ编译器认识到它必须在这里做一些工作。
package de.scrum_master.app;public interface MyInterface { void one(); void two(); public static aspect MarkerAnnotationInheritor { // Implementing classes should inherit marker annotation declare @type: MyInterface+ : @Marker; // Overriding methods 'two' should inherit marker annotation declare @method : void MyInterface+.two() : @Marker; }}


