Java Reference
In-Depth Information
The generated file is an empty Java class:
package com.ensode.jsf.namedbeans;
public class RegistrationBean {
}
To turn the preceding Java class into a CDI named bean, all we need to do is annotate
it with the @Named annotation. The @Named annotation marks the class as a CDI
named bean. By default, the CDI named bean name defaults to the class name (in our
case, RegistrationBean ) with its first character switched to lower case (in our case,
registrationBean ). If we want to override the default name, we can do it by setting
the value attribute of the @Named annotation to the desired value. In general, sticking
to the defaults allows more readable and maintainable code; therefore, we shouldn't
deviate from them unless we have a good reason.
The CDI named beans can have different scopes. A scope of request means that the
bean is only available in a single HTTP request. CDI named beans can also have a
session scope, in which case they are available in a single user's HTTP session. CDI
named beans can also have a conversation scope, in which the bean is available
across several HTTP requests.
The CDI named beans can also have a scope of application, which means that the
bean is accessible to all users in the application, across user sessions. A CDI named
bean can have a dependent pseudo-scope, in which case a new instance is injected
any time it is required.
Finally, a CDI named bean can have a scope of flow, in which case the bean will be
available through a specific JSF flow (discussed later in this chapter). To give our CDI
named bean the desired scope, we need to decorate it with the appropriate annotation.
The following table summarizes possible CDI named bean scopes along with the
corresponding annotation:
Scope
Annotation
Request
@RequestScoped
Session
@SessionScoped
Conversation
@ConversationScoped
Application
@SessionScoped
Dependent
@Dependent
Flow
@FlowScoped
Search WWH ::




Custom Search