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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10293510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:25:09  来源:igfitidea点击:

What is the default scope of a Named CDI bean?

javadependency-injectioncdijboss-weldnamed

提问by kostja

Is there any default scope for a @NamedCDI bean without additional @...Scopedannotations? I have not found any relevant information in the official Weld documentation.

@Named没有附加@...Scoped注释的CDI bean是否有任何默认范围?我在官方 Weld 文档中没有找到任何相关信息。

A @Namedbean can be accessed over JSF without additional annotations, so some implicit scope seems likely.

一个@Namedbean可以在JSF访问而无需额外的注解,所以一些隐含的范围似乎有可能。

Thank you

谢谢

回答by Matt Handy

The default scope is the dependent pseudo-scope @Dependent, as stated in the weld documentation:

默认范围是从属伪范围@Dependent,如焊接文档中所述

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.

CDI 具有所谓的依赖伪作用域。这是未显式声​​明范围类型的 bean 的默认范围。[...] 不同客户端或不同注入点之间永远不会共享依赖 bean 的实例。它严格地是某个其他对象的依赖对象。它在它所属的对象被创建时被实例化,在它所属的对象被销毁时被销毁。

The javadoc for this annotationgives some more information about this scope:

此注释javadoc提供了有关此范围的更多信息:

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.

使用范围 @Dependent 声明的 Bean 与具有其他内置范围类型的 Bean 的行为不同。当 bean 被声明为具有作用域 @Dependent 时:

  • 没有注入的 bean 实例在多个注入点之间共享。
  • 注入到容器正在创建的对象中的 bean 的任何实例都绑定到新
    创建的对象的生命周期。
  • 当 JSF 或 JSP 页面中通过 EL 名称引用 bean 的统一 EL 表达式被求值时,最多实例化 bean 的一个实例。这个实例的存在只是为了服务于 EL 表达式的一次评估。如果 bean EL 名称
    在 EL 表达式中多次出现,则它会被重用,但在再次计算 EL 表达式或计算另一个 EL 表达式时永远不会重用
  • 接收生产者方法、生产者字段、处理者方法或观察者方法调用的 bean 的任何实例都存在
    ,仅用于为该调用提供服务。
  • 注入到处置器方法或观察者方法的方法参数中的 bean 的任何实例都存在,
    仅用于为方法调用提供服务。