命名 CDI bean 的默认范围是什么? [英] What is the default scope of a Named CDI bean?

查看:25
本文介绍了命名 CDI bean 的默认范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

@Named bean 可以通过 JSF 访问而无需额外的注解,因此某些隐式作用域似乎是可能的.

谢谢

解决方案

默认作用域是依赖的伪作用域 @Dependent,如 焊接文档:

<块引用>

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

此注释的 javadoc 提供了有关此范围的更多信息:<块引用>

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

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

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

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

Thank you

解决方案

The default scope is the dependent pseudo-scope @Dependent, as stated in the 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.

The javadoc for this annotation gives some more information about this scope:

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.

这篇关于命名 CDI bean 的默认范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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