简体   繁体   English

当注释具有参数时,Java EE CDI Interceptor不起作用

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

I want to write a CDI interceptor. 我想写一个CDI拦截器。 The interception works well if my annotation only contains 1 parameter but breaks if 2 parameters are used. 如果我的注释仅包含1个参数,则截取效果很好,但如果使用2个参数则会中断。 The question is why? 问题是为什么?

The interceptor class: 拦截器类:

@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();
    }
}

The annotation: 注释:

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

Now the interesting part: 现在有趣的部分:

@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; 
    }
}

I know that MyBean is not annotated with @Interceptors . 我知道MyBean没有使用@Interceptors注释。 This is intended. 这是有意的。 The interceptor is declared in beans.xml : 拦截器在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>

The parameters are part of the binding. 参数是绑定的一部分。 Either annotate the parameters with @Nonbinding , or make sure the same values are used for the interceptor as well as for the intercepting point. 使用@Nonbinding注释参数,或者确保为拦截器和拦截点使用相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM