Java Reference
In-Depth Information
Just like JSF managed beans, CDI named beans can have one of several scopes as
listed in the following table. The preceding named bean has a scope of request, as
denoted by the @RequestScoped annotation.
Scope Annotation Description
Request @RequestScoped Request scoped beans are shared through
the duration of a single request. A single
request could refer to an HTTP request, an
invocation to a method in an EJB, a web
service invocation, or sending a JMS message
to a message-driven bean.
Session @SessionScoped Session scoped beans are shared across all
requests in an HTTP session. Each user of an
application gets their own instance of a session
scoped bean.
Application @ApplicationScoped Application scoped beans live through the
whole application lifetime. Beans in this scope
are shared across user sessions.
Conversation @ConversationScoped The conversation scope can span multiple
requests, and is typically shorter than the
session scope.
Dependent @Dependent
Dependent scoped beans are not shared. Any
time a dependent scoped bean is injected, a
new instance is created.
As we can see, CDI has equivalent scopes to all JSF scopes. Additionally, CDI adds
two additional scopes. The first CDI-specific scope is the conversation scope , which
allows us to have a scope that spans across multiple requests, but is shorter than
the session scope. The second CDI-specific scope is the dependent scope , which
is a pseudo scope. A CDI bean in the dependent scope is a dependent object of
another object; beans in this scope are instantiated when the object they belong
to is instantiated and they are destroyed when the object they belong to is destroyed.
Our application has two CDI named beans. We already discussed the customer bean.
The other CDI named bean in our application is the controller bean:
package com.ensode.cdiintro.controller;
import com.ensode.cdiintro.model.Customer;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
Search WWH ::




Custom Search