开发者

Bean instance of a shorter scope injected in a bean instance of a larger scope in CDI - how does it work?

开发者 https://www.devze.com 2023-03-17 01:44 出处:网络
Consider the following request-scoped CDI bean: @RequestScoped public class RequestScopedBean { // ... } Now, I inject it in a application-scoped bean:

Consider the following request-scoped CDI bean:

@RequestScoped
public class RequestScopedBean {
    // ...
}

Now, I inject it in a application-scoped bean:

@ApplicationScoped
public class ApplicationScopedBean {
    @Inject private RequestScopedBean requestScopedBean;
    // ...
}

I ran this code and noted that the request-scoped bean in开发者_如何学Cstance is different between two requests but the application-scoped bean instance is the same. My doubt is: how does this work? Is the request-scoped bean instance reattributed to the application-scoped field at each request? Or the proxy of the application-scoped bean just changes between requests?


In CDI each injected object is actually a proxy. So in that case, the proxy probably holds a reference to the RequestContext and on each method invocation gets the correct bean instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消