簡體   English   中英

當注釋具有參數時,Java EE CDI Interceptor不起作用

[英]Java EE CDI Interceptor not working when annotation has parameters

我想寫一個CDI攔截器。 如果我的注釋僅包含1個參數,則截取效果很好,但如果使用2個參數則會中斷。 問題是為什么?

攔截器類:

@Monitored
@Interceptor
@Priority(APPLICATION)
public class MonitoringInterceptor {

    @AroundInvoke 
    public Object logInvocation(InvocationContext ctx) throws Exception {
        LOGGER.error("METHOD CALLED!!!"); //this is not called when annotation has 2 parameters
        return ctx.proceed();
    }
}

注釋:

@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Monitored {
    public String layer() default "BUSINESS";
    public String useCase() default "N/A";
}

現在有趣的部分:

@Stateless
public class MyBean {

    //this does not work, why?
    @Monitored(layer = "BUSINESS", useCase = "test")

    //if I use the following annotation it works well
    //@Monitored(layer = "BUSINESS")
    public String sayHello(String message) {
        return message; 
    }
}

我知道MyBean沒有使用@Interceptors注釋。 這是有意的。 攔截器在beans.xml聲明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all">
<interceptors>
    <class>my.package.MonitoringInterceptor</class>
</interceptors>
</beans>

參數是綁定的一部分。 使用@Nonbinding注釋參數,或者確保為攔截器和攔截點使用相同的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM