Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

java - What is the default scope of a Named CDI 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...