InterceptorBinding not working

I created a custom annotation as below

@InterceptorBinding @Retention(RUNTIME) @Target(TYPE, METHOD) public @interface Traceable {} 

I wrote an interceptor as shown below

 @Traceable @Interceptor public class EnterExitLogger { @AroundInvoke public Object aroundInvoke(InvocatiobContext c) {} } 

The interceptor and annotation are in a module called common-utils.

I annotated my target class with @Traceable at class level as below

 @Traceable public class CDIManagedBean { } 

I declared an interceptor entry in the beans.xml file as shown below

 <interceptors> <class>my.package.EnterExitLogger</class> </interceptors> 

The target class is in a separate module. beans.xml is located in the META-INF directory of the target class module.

The methods of the target class are called from the rest class. When I call methods, the AroundInvoke hook method is not called.

I read the documents and realized that the interceptor should contain a public argument constructor. I added. But still the interceptor was not called.

I added @ Mentioned custom annotation after reading documents. But still the interceptor was not called.

In the docs, I noticed that the interceptor implements the Serializable interface. Although not mentioned, I also implemented Serializable. Still not working.

Then I removed the user annotation from the interceptor, the beans.xml file and the target class. I also removed the public argument constructor from the interceptor and removed Serializable.

Then I annotated the target class @Interceptors(EnterExitLogger.class) and called the thread. My interceptor was called.

Can someone tell me how to do with InterceptorBinding?

PS

I am deploying my ear to a WAS 8.5 server.

+5
source share
2 answers

The problem was fixed after adding an empty beans.xml file to the module where the interceptors are stored. I thought a beans.xml file is needed when I want to use cdi injection.

+1
source

The Java EE Tutorial provides a nice explanation and a few examples of hooks:

Create an interceptor binding annotation that needs to be annotated with @Inherited and @InterceptorBinding :

 @Inherited @InterceptorBinding @Retention(RUNTIME) @Target({METHOD, TYPE}) public @interface Logged { } 

Create an incerceptor class that annotates with the interceptor binding annotation created above, as well as the @Interceptor annotation.

Each @AroundInvoke takes an InvocationContext returns an Object and throws an Exception . The @AroundInvoke method should call InvocationContext#proceed() , which calls the target class method:

 @Logged @Interceptor public class LoggedInterceptor implements Serializable { public LoggedInterceptor() { } @AroundInvoke public Object logMethodEntry(InvocationContext invocationContext) throws Exception { System.out.println("Entering method: " + invocationContext.getMethod().getName() + " in class " + invocationContext.getMethod().getDeclaringClass().getName()); return invocationContext.proceed(); } } 

After determining the type of interceptor and binding, you can annotate beans and individual methods with a binding type to indicate that the interceptor should be called in all bean methods or in specific methods.

For example, a PaymentHandler bean is annotated @Logged , which means that any call to its business methods will call the @AroundInvoke interceptor to call:

 @Logged @SessionScoped public class PaymentHandler implements Serializable {...} 

However, you can only intercept a set of bean methods, annotating only the methods you need:

 @Logged public String pay() {...} @Logged public void reset() {...} 

In order for the interceptor to be called in the CDI application, it must be specified in the beans.xml file:

 <interceptors> <class>your.package.LoggedInterceptor</class> </interceptors> 

If the application uses more than one interceptor, the interceptors are called in the order specified in the beans.xml file.

+5
source

Source: https://habr.com/ru/post/1234269/


All Articles