gpt4 book ai didi

java - Instance#get() 是否为 CDI 中的 @ApplicationScoped beans 返回相同的实例?

转载 作者:行者123 更新时间:2023-11-29 08:26:16 26 4
gpt4 key购买 nike

如果 @ApplicationScoped bean是通过Instance<T>#get()获取的,后续调用 get()重用同一个实例(我确定使用了相同的 ProxyObject)?

最佳答案

If an @ApplicationScoped bean is obtained through Instance<T>#get(), does subsequent calls to get() reuse the same instance?

简短回答:是的,将返回同一实例的代理。继续阅读以了解更多详细信息。


来自 Instance<T> 文档:

The inherited Provider.get() method returns a contextual references for the unique bean that matches the required type and required qualifiers [...]

请参阅 CDI specification 中的以下引用它定义了上下文引用:

5.4. Client proxies

An injected reference, or reference obtained by programmatic lookup, is usually a contextual reference as defined by Contextual reference for a bean.

A contextual reference to a bean with a normal scope, as defined in Normal scopes and pseudo-scopes, is not a direct reference to a contextual instance of the bean (the object returned by Contextual.create()). Instead, the contextual reference is a client proxy object. A client proxy implements/extends some or all of the bean types of the bean and delegates all method calls to the current instance (as defined in Normal scopes and pseudo-scopes) of the bean.

[...]

关于正常范围CDI specification提到以下内容:

Contexts with normal scopes must obey the following rule:

Suppose beans A, B and Z all have normal scopes. Suppose A has an injection point x, and B has an injection point y. Suppose further that both x and y resolve to bean Z according to the rules of typesafe resolution. If a is the current instance of A, and b is the current instance of B, then both a.x and b.y refer to the same instance of Z. This instance is the current instance of Z.

All normal scopes must be explicitly declared @NormalScope, to indicate to the container that a client proxy is required.

如果您检查 @ApplicationScoped 注释,你会发现它被注释为 @NormalScope :

@Target(value = { TYPE, METHOD, FIELD })
@Retention(value = RUNTIME)
@Documented
@NormalScope
@Inherited
public @interface ApplicationScoped

关于java - Instance#get() 是否为 CDI 中的 @ApplicationScoped beans 返回相同的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52698661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com