Java CDI: Do interceptors have scope?
Asked Answered
M

2

7

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 {
...
    }
Melena answered 19/7, 2014 at 16:32 Comment(0)
J
5

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.

Jockey answered 20/7, 2014 at 12:10 Comment(5)
Just a followup question... I was trying to Inject a @RequestScoped interceptor into an @ApplicationScoped interceptor on Apache TomEE. The requestscoped fired first, and stored a value. But when the appscoped interceptor fired, it got a new instance of the requestscoped interceptor injected into it. What gives?Melena
CDI 1.1 spec says that interceptors should be Dependent, otherwise, non portable behavior results. Weld 2.2.6, for instance, disallows interceptors with a scope other than Dependent and treats them as definition errors.Bernardinebernardo
@jpangamarca: Interesting. You should provide this as an answer, since this makes mine wrong. Will update.Jockey
@Bernardinebernardo Do you know if in a new CDI version specification is allowed to have Interceptors with non dependent scope?Agog
@Agog Not that I'm aware of.Bernardinebernardo
B
13

CDI 1.1 spec says that interceptors should be Dependent, otherwise, non portable behavior results. Weld 2.2.6, for instance, disallows interceptors with a scope other than Dependent and treats them as definition errors.

Bernardinebernardo answered 14/3, 2015 at 1:54 Comment(0)
J
5

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.

Jockey answered 20/7, 2014 at 12:10 Comment(5)
Just a followup question... I was trying to Inject a @RequestScoped interceptor into an @ApplicationScoped interceptor on Apache TomEE. The requestscoped fired first, and stored a value. But when the appscoped interceptor fired, it got a new instance of the requestscoped interceptor injected into it. What gives?Melena
CDI 1.1 spec says that interceptors should be Dependent, otherwise, non portable behavior results. Weld 2.2.6, for instance, disallows interceptors with a scope other than Dependent and treats them as definition errors.Bernardinebernardo
@jpangamarca: Interesting. You should provide this as an answer, since this makes mine wrong. Will update.Jockey
@Bernardinebernardo Do you know if in a new CDI version specification is allowed to have Interceptors with non dependent scope?Agog
@Agog Not that I'm aware of.Bernardinebernardo

© 2022 - 2024 — McMap. All rights reserved.