Java BeanUtils - Overview


Advertisements

Description

The Java BeanUtils are the components of the Apache Commons which are derived from JavaAPI and provides component architecture for the Java language. The Java BeanUtils design patterns uses utility classes that helps to get and set the property values on Java classes for retrieving and defining the bean properties.

The package org.apache.commons.beanutils contains tool called introspection that facilitates the use of getting and setting property values on Java classes and display them in a visual manner in the development tools.

JavaBeans Characteristics

The below listed are important characteristics of JavaBeans which are useful in the development structure:

  • The class should be public and gives a public constructor with no arguments. It allows tools and applications to create new instances of the bean dynamically, without knowing what type of Java class name will be used as shown below:

    String className = ...;
    Class beanClass = Class.forName(className);
    Object beanInstance = beanClass.newInstance();
    
  • The constructor which does not have arguments whose bean's behavior can be configured separately from its instantiation. This can be achieved by using properties of the bean and also used to modify its behavior or data which are displayed by the bean.

  • The bean property contains setter and getter methods which are used to access the property values. The design pattern for these properties can be specified by using the set or get prefix for the property names along with the first character capitalized by using the JavaBeans specification. For instance, you can use setter and getter methods for the properties first_name and last_name as shown below:

    public class Employee {
       public Employee();   // Zero-arguments constructor
       public String getFirstName();
       public void setFirstName(String first_name);
       public String getLastName();
       public void setLastName(String last_name);
       public String getFullName();
    }
    
  • If there are getter and setter methods for the property names, then the getter should match the setter datatype. In JavaBean specification, you can have more than one setter with same name, but with different property types.

  • There is no need to define the getter and setter methods for each property. In the above code, there is no setter method for fullName property and it is only a read only property.

  • You can create a JavaBean where there is no match for naming pattern by using the getter and setter methods. The JavaBean support classes in the Java language and BeanUtils package to specify the property method names in the BeanInfo class along with the bean class.

  • The JavaBeans specification provides design patterns for event listeners, combines JavaBeans into component hierarchies and other helpful features of the BeanUtils package.

External Dependencies

You can use the following external dependencies for the commons-beanutils package:

Advertisements