Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 457
kostja Avatar asked Apr 24 '12 07:04

kostja


People also ask

What is CDI scope?

CDI places beans of contextual scope in the context whose lifecycle is defined by the Java EE specifications. For example, a session context and its beans exist during the lifetime of an HTTP session. Injected references to the beans are contextually aware.

What are CDI managed beans?

A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.

What is application scope in Java?

Context/Application scope begins when a webapp is started and ends when it is shutdown or reloaded. Parameters/attributes within the application scope will be available to all requests and sessions. The Context/Application object is available in a JSP page as an implicit object called application.

How do you decide what should be the scope of the bean?

If you place a managed bean into request scope, a new instance is created with each request. It is worth considering request scope if you are concerned about the cost of session scope storage. ApplicationScope: The application scope persists for the entire duration of the web application.


1 Answers

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.
like image 71
Matt Handy Avatar answered Sep 29 '22 16:09

Matt Handy