Java Reference
In-Depth Information
Configuring Managed Beans
When a page references a managed bean for the first time, the JavaServer Faces imple-
mentation initializes it based on a
@ManagedBean
annotation in the bean class (or a
@Named
annotation for CDI managed beans) or according to its configuration in the ap-
plication configuration resource file. For information on using annotations to initialize
beans, see “
Using Annotations to Configure Managed Beans
”
on page
142
.
You can use either annotations or the application configuration resource file to instantiate
managed beans that are used in a JavaServer Faces application and to store them in scope.
The managed bean creation facility is configured in the application configuration resource
file using
managed-bean
XML elements to define each bean. This file is processed at
With the managed bean creation facility, you can:
• Create beans in one centralized file that is available to the entire application, rather
than conditionally instantiate beans throughout the application
• Customize a bean's properties without any additional code
• Customize a bean's property values directly from within the configuration file so
that it is initialized with these values when it is created
• Using
value
elements, set a property of one managed bean to be the result of
evaluating another value expression
This section shows you how to initialize beans using the managed bean creation facility.
See “Writing Bean Properties” in
The Java EE 6 Tutorial: Basic Concepts
and “Writing
Managed Bean Methods” in
The Java EE 6 Tutorial: Basic Concepts
for information on
programming managed beans.
Using the
managed-bean
Element
A managed bean is initiated in the application configuration resource file using a
managed-bean
element, which represents an instance of a bean class that must exist
in the application. At runtime, the JavaServer Faces implementation processes the
managed-bean
element. If a page references the bean, and if no bean instance exists,
the JavaServer Faces implementation instantiates the bean as specified by the element
configuration.
Here is an example managed bean configuration from the Duke's Bookstore case study: