Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

3.18. Creating a Dynamic Bean

3.18.1. Problem

You need to be able to create a bean dynamically at runtime.

3.18.2. Solution

Use a DynaBean. You can create a DynaBean with an arbitrary set of properties at runtime, and the resulting DynaBean object will function properly with all Commons BeanUtils utilities, such as PropertyUtils. The following example demonstrates the use of a BasicDynaBean to model a politician:

import java.util.*;
import org.apache.commons.beanutils.*;
DynaProperty[] beanProperties = new DynaProperty[]{
    new DynaProperty("name", String.class),
    new DynaProperty("party", Party.class),
    new DynaProperty("votes", Long.class)
};
BasicDynaClass politicianClass = 
    new BasicDynaClass("politician", BasicDynaBean.class, props);
DynaBean politician = politicianClass.newInstance( );
// Set the properties via DynaBean
politician.set( "name", "Tony Blair" );
politician.set( "party", Party.LABOUR );
politician.set( "votes", new Long( 50000000 ) );
// Set the properties with PropertyUtils
PropertyUtils.setProperty( politician, "name", "John Major" );
PropertyUtils.setProperty( politician, "party", Party.TORY );
PropertyUtils.setProperty( politician, "votes", new Long( 50000000 ) );

In this code, the properties of the politician bean are set using two different methods. The first method is to manipulate properties via the DynaBean interface, and the second method involves using PropertyUtils.setProperty( ). Both regions of code accomplish the same goal, and PropertyUtils was included to emphasize the fact that most utilities in BeanUtils will understand how to work with DynaBean implementations.

3.18.3. Discussion

DynaBean objects come in handy when your system uses beans to represent a data model. Since a bean is just a collection of properties, you can avoid having to maintain a bean class by automatically generating a bean from a description of the objects and properties; for example, a complex data model could be described in an XML document, and a utility would parse such a document and create a number of DynaClass objects at runtime.

A DynaBean contains the methods listed in Table 3-2. There are methods to get and set indexed and mapped properties, and two operations—remove() and contains( )—allow you to manipulate the contents of a Map property.

Table 3-2. Methods available on a DynaBean

Method

Description

get(String name)

Retrieves a simple bean property

get(String name, int i)

Retrieves an indexed been property

get(String name, String key)

Retrieves a mapped bean property

set(String name, Object value)

Sets a simple bean property

set(String name, int i, Object value)

Sets an indexed bean property

set(String name, String key, Object value)

Sets a mapped bean property

remove(String name, String key)

Removes a key from a mapped bean property

contains(String name, String key)

Tests a map property for the presence of a key


3.18.4. See Also

Chapter 6 combines the power of Commons Digester and Commons BeanUtils to create a utility that reads in bean definitions from an XML document. A data model is described using an XML document, and it is realized into a set of DynaClass objects.

Chapter 12 discusses the power of Commons BeanUtils as it relates to working with a database. A ResultSetDynaClass enables you to wrap a JDBC ResultSet.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.