|
In many ways, BeanUtils can be considered a metadata wrapper that makes it as easy to work with a JavaBean as a Map. The properties are the keys, and property values can be set by simply setting the property as a value. For example: myUser.setName("Bob"); ...can instead be written: BeanUtils.setProperty(myUser, "name", "Bob"); Similarly, an array of the properties available for myUser can be simply retrieved: DynaProperty[] properties = WrapDynaClass. createDynaClass(myUser.class).getDynaProperties(); This offers two key advantages: it allows you to decouple components of your application, and it allows you to build frameworks and tools to take advantage of the JavaBeans framework. As shown in Figure 7-1, the BeanUtils package draws a distinction between a DynaClass, which describes a class, and a DynaBean, which describes a particular object instance. This notion can actually be extended a bita DynaClass can be used as a wrapper for a JDBC ResultSet, for example (in which the properties correspond to the returned results), and individual records can be returned as DynaBeans. Figure 7-1. BeanUtils class diagram.One aspect of the BeanUtils package worth calling out is the notion of a Converter. This provides a generic way to retrieve and set values across a suite of properties using String values, regardless of the type of the property. For example, you may want to set a property with a type of int using a String such as "2". The Converter package takes care of these details for you. The following types are supported by built-in converters:
|
|