What is the default scope of a Named CDI bean?
Asked Answered
C

1

27

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

Cogon answered 24/4, 2012 at 7:24 Comment(0)
C
48

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.
Consumerism answered 24/4, 2012 at 7:38 Comment(2)
Thank you, Matt. Have totally missed the part in the 1.0 docs. My bad.Cogon
No problem! Good to hear that it helped!Consumerism

© 2022 - 2024 — McMap. All rights reserved.