Dynamic Beans is an extension of the standard JavaBeans specification that allows a Java object to expose different sets of properties, which are determined at runtime. Unlike Java Beans, any object may be treated as a Dynamic Bean, and its properties may be determined at runtime. This mechanism, for example, allows the Profile
object to be used in a DSP tag like this:
<dsp:valueof bean="Profile.city"/>
even though there is no getCity()
method in the Profile
object. 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 may 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. Once 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, since 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");