vinga
vinga

Reputation: 2012

Interceptor binding doesn't work

I have CDI interceptor class annotated with javax.interceptor.InterceptorBinding @LogBinding, defined as follows:

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

Then I use annotation on EJB bean:

@Alternative
@Stateless
@LogBinding 
public class FooService {

    @Asynchronous
    public fooMethod() {

    }
}

I invoke fooMethod from another bean. The problem is that interceptor isn't called. Everything works when I change @LogBinding annotation to @Interceptors({LogInterceptor.class}) on the FooService. I don't know if it may have impact but FooService is market as @Alternative, because it's injected in other places as EJB bean, below is the producer field:

@Default
@Produces
@EJB
private FooService fooService;


@Interceptor
@LogBinding
@Priority(Interceptor.Priority.APPLICATION)
public class LogInterceptor {
  @AroundInvoke
    public Object invoke(final InvocationContext invocationContext) throws Exception {
    System.out.println("it works");
    }
}

Why it doesn't work?

Upvotes: 1

Views: 1343

Answers (1)

Trash Can
Trash Can

Reputation: 6814

You also have to annotate your interceptor class with that binding in order for it to know which class to use as the interceptor

@LogBinding
@Interceptor
public class LogInterceptor {...}

Upvotes: 1

Related Questions