2011-03-12 138 views
10

考慮我已經定義了以下方面的參數:Spring AOP的:獲取切入點註解

@Aspect 
public class SampleAspect { 

    @Around(value="@annotation(sample.SampleAnnotation)") 
    public Object display(ProceedingJoinPoint joinPoint) throws Throwable { 
     // ... 
    } 
} 

和註釋

public @interface SampleAnnotation { 
    String value() default "defaultValue"; 
} 

有沒有辦法讀取註釋SampleAnnotation參數的值在顯示方法,如果我的方面?

感謝您的幫助, 埃裏克

+1

[訪問註解值的建議](http://stackoverflow.com/questions/5026247/accessing-annotation-value-in-advice) – axtavt 2011-03-12 11:45:37

回答

10

更改建議的簽名

@Around(value="@annotation(sampleAnnotation)") 
public Object display(ProceedingJoinPoint joinPoint, SampleAnnotation sampleAnnotation) throws Throwable { 
    // ... 
} 

,你將有機會獲得在註釋的值。

有關更多信息,請參閱docs

+1

可能的重複深入文檔檢查:http:// docs .spring.io/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html - 7.2.4.6節建議參數 – hoodieman 2015-04-21 11:15:52