I want to write a CDI interceptor. The interception works well if my annotation only contains 1 parameter but breaks if 2 parameters are used. 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
. This is intended. The interceptor is declared in 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With