Java Reference
In-Depth Information
As you can see in this listing, the name of the Employee bean is employee per the bean-
naming conventions in Java. As a result, the first letter is lowercase. Without the @Named
annotation, JSF would have been unable to resolve employee .
Let's next look at scoping. We've discussed contexts and scoping earlier in the chapter.
Listing 12.2 contained a @RequestScoped annotation that may have seemed a little
mysterious.
12.2.3. Bean scoping
Earlier we discussed the concept of contexts and scopes; both terms are used interchange-
ably. CDI comes with four scopes and two pseudo-scopes. Every bean is associated with a
scope and the scope determines the lifecycle of the bean. When the scope is destroyed, so
is the bean. The scope of a bean is configured using annotations that are placed on the class
or a producer method (we'll cover this shortly). The annotations for the different scopes are
documented in table 12.1 .
Table 12.1. Scopes/contexts in CDI
Scope
Description
An instance is created only once for the duration of the application and is destroyed once
the application is terminated.
@ApplicationScoped
An instance is created each time an injection is performed. This is the default scope of a
bean and is used the vast majority of the time.
@Dependent
This is a new scope type that was introduced with CDI but existed in JBoss's Seam. It's a
scope that's programmatically controlled by the application. It spans multiple requests but
is shorter than the session scope. In a web application, conversations are used for a task
that spans multiple page requests. For example, this is how you'd handle the situations
where a user is booking two different vacations in two different browser tabs.
@ConversationScoped
This scope corresponds to the standard HTTP request. It begins when a request arrives
and is discarded when the response is rendered.
@RequestScoped
This scope corresponds to the HTTP session. References live for the duration of the ses-
sion.
@SessionScoped
If no annotation is placed on a bean, the bean is automatically assigned to the dependent
scope. This means that a new instance of the bean will be created each time the bean is in-
jected. The vast majority of the beans in an application will probably be dependent-scoped.
For web applications, JSF-backing beans should be in either the request or the conversation
scope.
 
Search WWH ::




Custom Search