在CDI中更大范围的bean实例中注入更短范围的Bean实例 - 它是如何工作的? [英] Bean instance of a shorter scope injected in a bean instance of a larger scope in CDI - how does it work?

查看:86
本文介绍了在CDI中更大范围的bean实例中注入更短范围的Bean实例 - 它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下请求范围的CDI bean:

Consider the following request-scoped CDI bean:

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

现在,我将它注入应用程序范围的bean中:

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

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

我运行此代码并注意到请求范围的bean实例在两个请求之间是不同的,但应用程序范围的bean实例是相同的。我怀疑的是:这是如何运作的?请求范围的bean实例是否在每次请求时重新分配到应用程序范围的字段?或者应用程序范围的bean的代理只是在请求之间发生变化?

I ran this code and noted that the request-scoped bean instance 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?

推荐答案

在CDI中,每个注入的对象实际上都是一个代理。所以在这种情况下,代理可能会持有对 RequestContext 的引用,并且每个方法调用都会获得正确的bean实例。

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.

这篇关于在CDI中更大范围的bean实例中注入更短范围的Bean实例 - 它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆