English 中文(简体)
Java BeanUtils - Overview
  • 时间:2024-10-18

Java BeanUtils - Overview


Previous Page Next Page  

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 utipty 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 faciptates 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 psted are important characteristics of JavaBeans which are useful in the development structure:

    The class should be pubpc and gives a pubpc constructor with no arguments. It allows tools and apppcations 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 capitapzed 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:

    pubpc class Employee {
       pubpc Employee();   // Zero-arguments constructor
       pubpc String getFirstName();
       pubpc void setFirstName(String first_name);
       pubpc String getLastName();
       pubpc void setLastName(String last_name);
       pubpc 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 psteners, 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