Java EE 7 CDI bean scopes
26 February 2014
In this article we will cover the CDI bean scopes that were introduced by Java EE 7.
Introduction
Java EE 7 introduced a new set of CDI bean scopes. In the next sections we will summarize each one of the aforementioned scopes while providing links to other articles that will describe them in detail.
Note: For more information about other CDI bean scopes and relevant details like the CDI proxying mechanism or bean serialization, please see Java EE CDI bean scopes.
Overview
Scope | Description |
---|---|
TransactionScoped | As the name states, a TransactionScoped bean will spawn its life time through the duration of an active transaction. As soon as a bean of this type is referenced by the first time inside a transaction, the CDI container will create an instance of the bean and reuse it across the entire transaction life time, ie. if the bean is referenced again later in the same transaction, the container will provide the same instance to the caller. |
FlowScoped | FlowScoped beans are to be used inside JSF Flows. Flows are used to represent units of work and are defined by a contained set of views and beans. A FlowScoped bean will spawn its life time across the duration of the flow where it is referenced by the first time. |
ViewScoped | ViewScoped beans will spawn their life time across the duration of a JSF view. Since this kind of beans keep their state during the entire view life time, they become useful to build Ajax interactions between the client and the server in a conversational style. |
Detailed articles
The following articles cover the Java EE 7 CDI bean scopes in detail:
TransactionScoped beans: Java EE CDI TransactionScoped example
JSF Flows and FlowScoped beans: JSF Flow example
ViewScoped beans: Java EE CDI ViewScoped example