What is the default scope of a Named CDI bean?

2019-01-23 23:41发布

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

1条回答
Viruses.
2楼-- · 2019-01-24 00:16

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.
查看更多
登录 后发表回答