You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by LG Optimusv <lg...@gmail.com> on 2013/12/08 13:53:50 UTC

How to use parameters in interceptor annotations in CDI?

Hi All,

I created a CDI interceptor like this:

@InterceptorBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyInterceptorAnnotation {
//    MyInterceptorType interceptorType() default MyInterceptorType.ONE;
}

public enum MyInterceptorType { ONE, TWO, THREE }

@MyInterceptorAnnotation
@Interceptor
public class MyInterceptor {
    @AroundInvoke
    public Object theOnlyMethodInThisInterceptor(InvocationContext
invocationContext) throws Exception {
        Object target = invocationContext.getTarget();
        Method method = invocationContext.getMethod();
        MyInterceptorAnnotation myInterceptorAnnotation =
method.getAnnotation(MyInterceptorAnnotation.class);

        System.out.println("You're passing this interceptor with " +
/*myInterceptorAnnotation.interceptorType() +*/
                " for " + target.getClass().getName() + "." +
method.getName());

        return invocationContext.proceed();
    }
}

If I use parameters with the interceptor annotation (by removing all the
commented parts), the @AroundInvoke method isn't executed. Does anybody
know what my problem is?

Thanks.

PS: the project (which is very small) is at
https://github.com/dxxvi/cdi-exploring

Re: How to use parameters in interceptor annotations in CDI?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Add @NonBinding on your interceptor binding methods otherwise it is used to
know if the interceptor matches or not the invocation
Le 8 déc. 2013 13:54, "LG Optimusv" <lg...@gmail.com> a écrit :

> Hi All,
>
> I created a CDI interceptor like this:
>
> @InterceptorBinding
> @Target({ElementType.METHOD, ElementType.TYPE})
> @Retention(RetentionPolicy.RUNTIME)
> @Documented
> public @interface MyInterceptorAnnotation {
> //    MyInterceptorType interceptorType() default MyInterceptorType.ONE;
> }
>
> public enum MyInterceptorType { ONE, TWO, THREE }
>
> @MyInterceptorAnnotation
> @Interceptor
> public class MyInterceptor {
>     @AroundInvoke
>     public Object theOnlyMethodInThisInterceptor(InvocationContext
> invocationContext) throws Exception {
>         Object target = invocationContext.getTarget();
>         Method method = invocationContext.getMethod();
>         MyInterceptorAnnotation myInterceptorAnnotation =
> method.getAnnotation(MyInterceptorAnnotation.class);
>
>         System.out.println("You're passing this interceptor with " +
> /*myInterceptorAnnotation.interceptorType() +*/
>                 " for " + target.getClass().getName() + "." +
> method.getName());
>
>         return invocationContext.proceed();
>     }
> }
>
> If I use parameters with the interceptor annotation (by removing all the
> commented parts), the @AroundInvoke method isn't executed. Does anybody
> know what my problem is?
>
> Thanks.
>
> PS: the project (which is very small) is at
> https://github.com/dxxvi/cdi-exploring
>