background image

The CustomerBean Declaration

<< Initializing Array and List Properties | Initializing Maps and Lists >>
<< Initializing Array and List Properties | Initializing Maps and Lists >>

The CustomerBean Declaration

<property-name>streetAddress</property-name>
<value>#{addressBean}</value>
</managed-property>
<managed-property>
<property-name>customerType</property-name>
<value>New</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>addressBean</managed-bean-name>
<managed-bean-class>
com.mycompany.mybeans.AddressBean
</managed-bean-class>
<managed-bean-scope> none </managed-bean-scope>
<managed-property>
<property-name>street</property-name>
<null-value/>
<managed-property>
...
</managed-bean>
The first CustomerBean declaration (with the managed-bean-name of customer) creates a
CustomerBean
in request scope. This bean has two properties: mailingAddress and
streetAddress
. These properties use the value element to reference a bean named
addressBean
.
The second managed bean declaration defines an AddressBean but does not create it because its
managed-bean-scope
element defines a scope of none. Recall that a scope of none means that
the bean is created only when something else references it. Because both the mailingAddress
and the streetAddress properties reference addressBean using the value element, two
instances of AddressBean are created when CustomerBean is created.
When you create an object that points to other objects, do not try to point to an object with a
shorter life span because it might be impossible to recover that scope's resources when it goes
away. A session-scoped object, for example, cannot point to a request-scoped object. And
objects with none scope have no effective life span managed by the framework, so they can point
only to other none scoped objects.
Table 14­2
outlines all of the allowed connections.
TABLE 14­2
Allowable Connections between Scoped Objects
An Object of This Scope
May Point to an Object of This Scope
none
none
application
none
, application
session
none
, application, session
Configuring Beans
The Java EE 5 Tutorial · September 2007
446