Dynamic beans extend the standard JavaBeans specification, where a Java object can expose different sets of properties, that are determined at runtime. Unlike Java Beans, any object can be treated as a dynamic bean whose properties can be determined at runtime. This mechanism, for example, allows the Profile
object to be used in a DSP tag like the following, though there is no getCity()
method in the Profile
object.:
<dsp:valueof bean="Profile.city"/>
Dynamic beans lets you use the Profile
class without having to extend it to include as JavaBean properties all the profile attributes your application requires. Dynamic beans can be used in two broad sets of cases, where:
All beans of a class have the same properties, but the properties are not known at runtime. The
Profile
object is an example.Different instances of a class have different properties from each other. For example, in a
Hashtable
or aMap
, the properties are whatever keys are in the specific instance you are looking at.
A dynamic bean can be of any class, and need not implement any special interfaces. Before you can access a dynamic bean’s properties, an implementation of DynamicPropertyMapper
must be registered for the bean’s class, one of the bean’s superclasses, or one of the bean’s interfaces. DynamicPropertyMappers
are registered by default for several classes and interfaces that are most commonly used as dynamic beans, as listed in the next section, Registered DynamicBeans and Dynamic Types. You can register such an implementation by calling DynamicBeans.registerPropertyMapper()
. See Registering Dynamic Beans for more detailed information. After this has been done, you can use the methods DynamicBeans.getPropertyValue()
and DynamicBeans.setPropertyValue()
to access dynamic properties of objects belonging to the registered class or interface. This indirect approach permits existing classes like java.util.Hashtable
or interfaces like java.sql.ResultSet
to be treated as dynamic beans. If no DynamicPropertyMapper
is registered, these methods simply access the object’s regular JavaBean properties.
For example, because atg.userprofiling.Profile
is registered as a dynamic bean, one way to access the Profile.city
value from Java is:
String city = (String) DynamicBeans.getPropertyValue(profile, "city");
DynamicBeans
also has getSubPropertyValue()
and setSubPropertyValue()
methods, which take a hierarchy property name of the form propertyName1.subPropertyName2.subSubPropertyName3
. For example:
String city = (String) DynamicBeans.getSubPropertyValue(profile,
"homeAddress.country");