Java BeanUtils - Background



The standard JavaBeans of Java language can be used to access the property values of beans using the proper getter methods. The Java language supplies the java.beans.Introspector class to inspect a Java class at runtime. This indicates the property names of getter and setter methods along with the Reflection abilities to call such methods dynamically. You can make use of getting and setting bean properties dynamically by using the API's in the BeanUtils package.

The JavaBean property types are divided into three types (Some property types are supported by the JavaBeans specification and some are supported by the BeanUtils package):

  • Simple: The simple properties contain single value which can be retrieved or altered. You can use property type like Java language primitive such as int, a simple object such as java.lang.String, or complex object which is specified either by using the Java language, an application, or a class library with the application.

  • Indexed: An ordered collection of objects can be stored in the indexed property which can be accessed individually by using an integer-valued, non-negative index or subscript. The BeanUtils package includes datatype called java.util.List must be indexed in the JavaBeans specification.

  • Mapped: The BeanUtils package contains datatype called java.util.Map which should be mapped in the standard JavaBeans APIs and the individual values can be set and accessed by using a String-valued key.

You can get and set the property values for the datatypes by using the API methods specified in the PropertyUtils class. Consider the below code snippet of two bean classes defined with getter and setter methods:

public class Employee {
   public FullName getFullName();
   public void setFullName(String type, FullName fullname);
   public Employee getSubordinate(int index);
   public void setSubordinate(int index, Employee subordinate);
   public String getFirstName();
   public void setFirstName(String first_name);
   public String getLastName();
   public void setLastName(String last_name);