gpt4 book ai didi

java - 命名 CDI bean 的默认范围是什么?

转载 作者:IT老高 更新时间:2023-10-28 20:32:57 25 4
gpt4 key购买 nike

没有额外的 @...Scoped 注释的 @Named CDI bean 是否有任何默认范围?我在 official Weld documentation 中没有找到任何相关信息.

一个 @Named bean 可以通过 JSF 访问而无需额外的注释,因此似乎有一些隐式范围。

谢谢

最佳答案

默认作用域是依赖伪作用域@Dependent,如weld documentation 中所述。 :

CDI features the so-called dependent pseudo-scope. This is the default scope for a bean which does not explicitly declare a scope type. [...] An instance of a dependent bean is never shared between different clients or different injection points. It is strictly a dependent object of some other object. It is instantiated when the object it belongs to is created, and destroyed when the object it belongs to is destroyed.

javadoc for this annotation提供有关此范围的更多信息:

Beans declared with scope @Dependent behave differently to beans with other built-in scope types. When a bean is declared to have scope @Dependent:

  • No injected instance of the bean is ever shared between multiple injection points.
  • Any instance of the bean injected into an object that is being created by the container is bound to the lifecycle of the newly
    created object.
  • When a Unified EL expression in a JSF or JSP page that refers to the bean by its EL name is evaluated, at most one instance of the bean is instantiated. This instance exists to service just a single evaluation of the EL expression. It is reused if the bean EL name
    appears multiple times in the EL expression, but is never reused when the EL expression is evaluated again, or when another EL expression
    is evaluated.
  • Any instance of the bean that receives a producer method, producer field, disposer method or observer method invocation exists to
    service that invocation only.
  • Any instance of the bean injected into method parameters of a disposer method or observer method exists to service the method
    invocation only.

关于java - 命名 CDI bean 的默认范围是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293510/

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