background image

Configuring Beans

<< The faces-config Tag | Using the managed-bean Element >>
<< The faces-config Tag | Using the managed-bean Element >>

Configuring Beans

Configuring Beans
To instantiate backing beans and other managed beans used in a JavaServer Faces application
and store them in scope, you use the managed bean creation facility. This facility is configured
in the application configuration resource file using managed-bean XML elements to define each
bean. This file is processed at application startup time. When a page references a bean, the
JavaServer Faces implementation initializes it according to its configuration in the application
configuration resource file.
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 the bean's properties without any additional code.
Customize the 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 the 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" on page 379
and
"Writing Backing Bean Methods" on page 404
for
information on programming backing beans.
"Binding Component Values and Instances to
External Data Sources" on page 364
explains how to reference a managed bean from the
component tags.
Using the managed-bean Element
You create a bean 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, the JavaServer Faces implementation
instantiates the bean as specified by the element configuration if no instance exists.
Here is an example managed bean configuration from the Duke's Bookstore application:
<managed-bean>
<managed-bean-name> NA </managed-bean-name>
<managed-bean-class>
com.sun.bookstore6.model.ImageArea
</managed-bean-class>
<managed-bean-scope> application </managed-bean-scope>
<managed-property>
<property-name>shape</property-name>
<value>poly</value>
</managed-property>
Configuring Beans
Chapter 14 · Configuring JavaServer Faces Applications
439