2012-04-04 57 views
0

我需要攔截writer.write(myObj)類型的所有方法,並且當且僅當myObj包含用@BeforeWrite註解的方法時,才執行此方法。在@之前實現/ @用Spring AOP發佈一些東西

我有點困惑,因爲我可以攔截作家的方法調用,但我不知道如何提供一個切入點,看看是否有@BeforeWriter註釋的方法,這看起來類似於可能的在JPA中處理@PostLoad註釋...

回答

1

在您的攔截器處理方法中,您有參數類型爲ProceedingJoinPoint,其方法爲getArgs()。你可以檢查你的論點(myObj)的方法反思,並作出決定是否繼續。示例(callProceedingJoinPoint類型):

boolean proceed = false; 
for (Method method : call.getArgs()[0].getClass().getMethods()) { 
    if (method.isAnnotationPresent(BeforeWriter.class)) { 
     proceed = true; 
     break; 
    } 
} 
+0

我明白,我必須在方法循環,​​以驗證它的存在,對吧? – Edmondo1984 2012-04-11 09:51:05

+0

如果您不喜歡在集合上循環,並且喜歡使用謂詞方法,請嘗試[Apache collection util](http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html)。 – 2012-04-11 10:44:04