简体   繁体   中英

Exclude a specific method to be invoked by CDI Interceptor

I have an interceptor binding annotation :

@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyBinding {
}

To this CDI interceptor :

@Interceptor
@MyBinding 
public class MyInterceptor  {

    @AroundInvoke
    public Object applyPolicy(InvocationContext ctx) throws Exception {
        return blablabla;
    }
}

And a class annotated, that mean every methods of this class will invoke MyInterceptor

@MyBinding
public class GlobalController {

    public void methodA() {...}
    public void methodB() {...}
}

All works fine, but I wish methodB not invoking my interceptor.

I tried both annotations @ExcludeClassInterceptors and @ExcludeDefaultInterceptors on my method but it doesn't works for me. I think these annotations are especially for exclude a method for EJB Interceptor, and not CDI Interceptor with Interceptor binding.

Not sure about these annotations but as a workaround you can add an annotation to the method you want to exclude. Get Method from InvocationContext in the interceptor and check whether the method has the annotation. In this case just delegate to the original method.

Try @MyBinding at method level:

public class GlobalController {

  @MyBinding    
  public void methodA() {...}

  public void methodB() {...}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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