Search code examples
javajakarta-eecdiguiceweld

Java CDI: Do interceptors have scope?


What is the scope of an interceptor in CDI?

aka, is this legal? Would I get the same instance of this interceptor every place it's invoked?

@RequestScoped
public class SalesForceControllerInterceptor {
    @Inject
    private Logger log;

    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
...
    }

Solution

  • Yes, interceptors have a lifecycle like any other cdi managed bean ... so they are dependent by default but you can annotate them with any any scope you need. In your example, all calls within the same Request share the interceptor. If what you need is "the same interceptor for every call", you should consider a broader scope like session or application.

    Update: check the comments: with cdi 1.1 interceptors have to be of dependent scope and Weld 2.2.6 treats other scopes as errors.