[英]how to get the Annotated object using aspectJ
我有這樣的注釋:
@Inherited
@Documented
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Restful {
}
我這樣注釋這個類:
@Restful
public class TestAspect {
public String yes;
}
我有一個這樣的切入點:
@Pointcut("@annotation(com.rest.config.Restful)")
public void pointCutMethod() {
}
我試過:
@Before("pointCutMethod()")
public void beforeClass(JoinPoint joinPoint) {
System.out.println("@Restful DONE");
System.out.println(joinPoint.getThis());
}
但是 getThis() 返回 null。
基本上我正在嘗試獲取 TestAspect 的 Object 實例。 我該怎么做? 有什么線索嗎? 任何幫助將非常感激。
提前致謝
將您的注解放在類型上,切入點@annotation(com.rest.config.Restful)
您只會匹配您的類型的靜態初始化連接點。 正如我們在編譯時是否使用-showWeaveInfo
(我將您的代碼示例放入一個名為 Demo.java 的文件中):
Join point 'staticinitialization(void TestAspect.<clinit>())' in
Type 'TestAspect' (Demo.java:9) advised by before advice from 'X' (Demo.java:19)
當靜態初始值設定項運行時沒有this
,因此當你從thisJoinPoint
檢索它時你會得到 null 。 您還沒有說出您真正想要建議的內容,但讓我假設它是創建 TestAspect 的新實例。 您的切入點需要匹配此帶注釋類型的構造函數的執行:
// Execution of a constructor on a type annotated by @Restful
@Pointcut("execution((@Restful *).new(..))")
public void pointcutMethod() { }
如果你想匹配那種類型的方法,它會是這樣的:
@Pointcut("execution(* (@Restful *).*(..))")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.