java - InterceptorBinding is not working -


i created custom annotation below

@interceptorbinding @retention(runtime) @target(type, method) public @interface traceable {} 

i wrote interceptor below

@traceable @interceptor public class enterexitlogger {     @aroundinvoke     public object aroundinvoke(invocatiobcontext c) {} } 

the interceptor , annotation in module called common-utils.

i annotated target class @traceable @ class level below

@traceable public class cdimanagedbean { } 

i declared interceptor entry in beans.xml file below

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

the target class in separate module. beans.xml in meta-inf directory of target class's module.

the target class's methods called rest class. when invoke methods interceptor's aroundinvoke method not called.

i read docs , understood interceptor should contain public no argument constructor. added it. still interceptor not called.

i added @inherited on custom annotation after reading docs. still interceptor not called.

from docs noticed interceptor implementing serializable interface. although not mentioned implemented serializable also. still did not work.

then removed custom annotation interceptor, beans.xml file , target class. removed public no argument constructor interceptor , removed serializable.

then annotated target class @interceptors(enterexitlogger.class) , invoked flow. interceptor called.

can tell me how do interceptorbinding?

p.s.

i deploying ear in 8.5 server.

the java ee tutorial provides nice explanation , few examples interceptors:

create interceptor binding annotation, must annotated @inherited , @interceptorbinding:

@inherited @interceptorbinding @retention(runtime) @target({method, type}) public @interface logged { } 

create incerceptor class annotated interceptor binding annotation created above @interceptor annotation.

every @aroundinvoke method takes invocationcontext argument, returns object, , throws exception. @aroundinvoke method must call invocationcontext#proceed() method, causes target class method invoked:

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

once interceptor , binding type defined, can annotate beans , individual methods binding type specify interceptor invoked either on methods of bean or on specific methods.

for example, paymenthandler bean annotated @logged, means invocation of business methods cause interceptor's @aroundinvoke method invoked:

@logged @sessionscoped public class paymenthandler implements serializable {...} 

however can intercept set of methods of bean annotating desired methods:

@logged public string pay() {...}  @logged public void reset() {...} 

in order interceptor invoked in cdi application, must specified in beans.xml file:

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

if application uses more 1 interceptor, interceptors invoked in order specified in beans.xml file.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -