效仿註釋繼承人們經常問的AspectJ類似這樣的問題,所以我想回答它在一個地方,我可以很容易地鏈接到更高版本。與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 Marker {}
現在我詮釋這樣的接口和/或方法:
package de.scrum_master.app;
@Marker
public interface MyInterface {
void one();
@Marker void two();
}
這裏是一個小驅動器應用程序,它也實現了接口:
現在,當我定義這方面,我希望它被觸發
- 一個被註解類的每個構造函數執行和
- 一個註解的方法的每次執行。
package de.scrum_master.aspect;
import de.scrum_master.app.Marker;
public aspect MarkerAnnotationInterceptor {
after() : execution((@Marker *).new(..)) && !within(MarkerAnnotationInterceptor) {
System.out.println(thisJoinPoint);
}
after() : execution(@Marker * *(..)) && !within(MarkerAnnotationInterceptor) {
System.out.println(thisJoinPoint);
}
}
不幸的是,一方面打印什麼,就好像類Application
和方法two()
沒有任何@Marker
註解。 AspectJ爲什麼不攔截它們?
謝謝。可以增強動態發現註釋的方法嗎? – NeilS
@NeilS,我不明白這個問題。 – kriegaex
在你的例子中,你的方面專門處理名爲'two'的方法。它具體到'兩',不可重用於其他情況。有沒有一種方法可以選擇任何已經使用某些註釋進行註釋的方法,而不是直接按名稱來提及它們? – NeilS