Java Reference
In-Depth Information
CDI scopes
CDI Beans come with a set of predefined scopes and annotations, and each CDI Bean has a
distinct life cycle determined by the scope it belongs to. The following table describes the
built-in CDI scopes and annotations required to set these scopes:
Scope
Description
The @RequestScoped beans are shared during the length of a single request. This could be an HTTP request, a
remote EJB invocation, a web services invocation, or message delivered to a Message Driven Bean (MDB).
These beans are destroyed at the end of the request.
@RequestScoped
The @ConversationScoped beans are shared across multiple requests in the same HTTP session but only if
there is an active conversation maintained. Conversations are supported for JSF requests through the
javax.enterprise.context.Conversation bean.
@ConversationScoped
The @SessionScoped beans are shared between all the requests that occur in the same HTTP session and des-
troyed when the session is destroyed.
@SessionScoped
An @ApplicationScoped bean will live for as long as the application is running and be destroyed when the
application is shut down.
@ApplicationScoped
The @Dependent beans are never shared between injection points. Any injection of a dependent bean is a new
instance whose life cycle is bound to the life cycle of the object it is being injected into.
@Dependent
Other parts of Java EE can extend the list of available scopes. In Java EE 7 (in the Java
Transaction API specification), a new scope has been introduced: @TransactionS-
coped . It bounds the life cycle of a bean with the current transaction. It is of course pos-
sible to introduce your own custom scopes.
In this chapter example, we will use the RequestScoped and SessionScoped beans
to drive our simple ticket-booking system. In the next chapter, we will further enhance our
example using ConversationScoped beans, which are a peculiar scope of CDI Beans.
Providing a detailed explanation of all the named beans scopes is beyond the scope of this
book. However, you can quench your thirst for knowledge by having a look at CDI Refer-
ence Implementation (JBoss Weld) docs at http://docs.jboss.org/weld/reference/latest/en-
US/html/scopescontexts.html .
Search WWH ::




Custom Search