2017-04-11 68 views
0

我正在使用Spring AOP截取由@MyAnnotation註解的方法。攔截是好的。但是,不幸的是,我沒有得到我的註釋實例。Spring AOP by annotation pointcut annotation not retrieve

我譯註:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MyAnnotation { 
    String[] description(); 
} 

我的配置看點

@Aspect 
public class OAuthAspect { 

    @Pointcut(value = "execution(public * *(..))") 
    public void anyPublicMethod() { 
    } 

    @Pointcut(value = "@annotation(annotation)", argNames = "annotation") 
    public void anyAnnotationMethod(MyAnnotation annotation) { 
    } 

    @Around(value = "anyPublicMethod() && anyAnnotationMethod(annotation)") 
    public Object authorization(ProceedingJoinPoint pjp, MyAnnotation annotation) throws Throwable { 
     //annotation is null 
    } 
} 

例切入點:

@Service 
public class ContextService { 
    @MyAnnotation(description = {"de1", "des2"}) 
    public String getAll() { 
    } 
} 

我不明白爲什麼我無法檢索註釋實例。

如果有人有想法?

PC:編輯

回答

0

對我來說,類ContextService甚至不編譯,因爲在你的註釋裏有打字錯誤:String[] descrition();(注意失蹤「P」)確實應該String[] description();,然後再編譯,我還可以打印註釋實例。

+0

對於「描述」是合理的轉錄錯誤。在我的projet中,'p'存在並且我的projet編譯,但註解實例是null:'( –

+0

然後你有一個配置問題。我在AspectJ上測試了我的機器上的方面,並且它工作。如果你不同意,請提供完整的[MCVE](http://stackoverflow.com/help/mcve),包括通過GitHub構建的Maven。 – kriegaex

+0

好吧,它可以工作,我已經很難清理了我的projet和它沒關係,我認爲我在構建projet時遇到了問題,謝謝你的幫助:) –