Using Annotations to Configure Managed Beans
JavaServer Faces support for bean annotations is introduced in Chapter 4, JavaServer Faces Technology. Bean annotations can be used for configuring JavaServer Faces applications.
The @ManagedBean (javax.faces.bean.ManagedBean) annotation in a class automatically registers that class as a resource with the JavaServer Faces implementation. Such a registered managed bean does not need managed-bean configuration entries in the application configuration resource file.
An example of using the @ManagedBean annotation in a class is as follows:
@ManagedBean @SessionScoped public class DukesBday{ ... }
The above code snippet shows a bean that is managed by the JavaServer Faces implementation and is available for the length of the session. You do not need to configure the managed bean instance in the faces-config.xml file. In effect, this is an alternative to the application configuration resource file approach and reduces the task of configuring managed beans.
You can also define the scope of the managed bean within the class file, as shown in the above example. You can annotate beans with request, session, application, or view scope.
All classes will be scanned for annotations at startup unless the faces-config element in the faces-config.xml file has the metadata-complete attribute set to true.
Annotations are also available for other artifacts, such as components, converters, validators, and renderers, to be used in place of application configuration resource file entries. These are discussed, along with registration of custom listeners, custom validators, and custom converters, in Chapter 13, Creating Custom UI Components and Other Custom Objects.
Using Managed Bean Scopes
You can use annotations to define the scope in which the bean will be stored. You can specify one of the following scopes for a bean class:
Application (@ApplicationScoped): Application scope persists across all users’ interactions with a web application.
Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.
View (@ViewScoped): View scope persists during a user’s interaction with a single page (view) of a web application.
Request (@RequestScoped): Request scope persists during a single HTTP request in a web application.
None (@NoneScoped): Indicates a scope is not defined for the application.
Custom (@CustomScoped): A user-defined, nonstandard scope. Its value must be configured as a java.util.Map. Custom scopes are used infrequently.
You may want to use @NoneScoped when a managed bean references another managed bean. The second bean should not be in a scope (@NoneScoped) if it is supposed to be created only when it is referenced. If you define a bean as @NoneScoped, the bean is instantiated anew each time it is referenced, so it does not get saved in any scope.
If your managed bean is referenced by the binding attribute of a component tag, you should define the bean with a request scope. If you placed the bean in session or application scope instead, the bean would need to take precautions to ensure thread safety, because javax.faces.component.UIComponent instances each depend on running inside of a single thread.
If you are configuring a bean that allows attributes to be associated with the view, you can use the view scope. The attributes persist until the user has navigated to the next view.
Eager Application-Scoped Beans
Managed beans are lazily instantiated. That is, that they are instantiated when a request is made from the application.
To force an application-scoped bean to be instantiated and placed in the application scope as soon as the application is started and before any request is made, the eager attribute of the managed bean should be set to true as shown in the following example:
@ManagedBean(eager=true) @ApplicationScoped