2011-02-07 85 views
12

我使用AspectJ攔截被註解爲@Profile(description="something")與註釋AspectJ切入點參數

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Profile { 
    public String description() default ""; 
} 

@Around("com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods(profile)") 
public Object profile(ProceedingJoinPoint pjp, Profile profile) throws Throwable { 
    .... 
} 

@Pointcut("@annotation(com.merc.annotations.Profile)") 
protected void logAnnotatedMethods(Profile profile) { 
} 

方法,但同時compileing使用AJC

formal unbound in pointcut 
+0

嗨,我的要求和你一樣。我有一個疑問是什麼是'com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods'。我注意到,你創建的logAnnotatedMethods,但我沒有得到什麼是com.merc.aop.ctw.aspect.PointcutDefinitions?請指導我。 – James 2015-07-07 18:19:13

回答

14
@Pointcut("@annotation(com.merc.annotations.Profile)") 
protected void logAnnotatedMethods(Profile profile) { 
} 

這是不正確的,@annotation()想要一個參數名稱,而不是參數類型。

如果您的類是使用調試代碼編譯的,則pointcut參數必須與方法參數具有相同的名稱,如果不是,則需要依賴參數類型是唯一的,或者使用argNames明確寫出參數名稱參數:

@Pointcut(value="@annotation(profile)",argNames="profile") 
protected void logAnnotatedMethods(Profile arg) { } 

參考:

+0

我不同意。只是使用了註解類型@annotation作爲參數,並且它像Spring 3.1中的魅力一樣,正如文檔所說的那樣。 http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html – 2013-03-12 13:45:24

5

我打得周圍,我得到以下錯誤味精發現以下工作

@Pointcut("@annotation(profile)") 
protected void logAnnotatedMethods(Profile profile) { 
}