Java BeanUtils - Basic Property Access


Advertisements

Description

You can access the basic properties by using the following ways:

  • Simple Property

  • Indexed Property

  • Mapped Property

Simple Property

You can get and set the simple property values by using the below API signatures:

  • PropertyUtils.getSimpleProperty(Object, String)

  • PropertyUtils.SetSimpleProperty(Object, String, Object)

Parameters:

  • Object: It is a bean object that specifies the bean property to be extracted.

  • String: It is a string name that specifies the name of the property to be extracted.

Indexed Property

You can use two options for creating indexed properties; first option is building the subscript into property name and second option is defining the subscript in a separate argument to call the method.

The indexed properties can be get and set by using the below methods:

  • PropertyUtils.getIndexedProperty(Object, String)

  • PropertyUtils.getIndexedProperty(Object, String, int)

  • PropertyUtils.setIndexedProperty(Object, String, Object)

  • PropertyUtils.setIndexedProperty(Object, String, int, Object)

Parameters:

  • Object: It is a bean object that specifies the bean property to be extracted.

  • String: It is a string name that specifies the name of the property to be extracted.

  • int: It sets an index of the property value.

  • Object: It specifies the value for an indexed property element.

Mapped Property

You can get and set the mapped property values by using the below API signatures. If you have any extra argument, then it can be written within parentheses as ("(" and ")") instead of using square brackets.

  • PropertyUtils.getMappedProperty(Object, String)

  • PropertyUtils.getMappedProperty(Object, String, String)

  • PropertyUtils.setMappedProperty(Object, String, Object)

  • PropertyUtils.setMappedProperty(Object, String, String, Object)

Parameters:

  • Object: It is a bean object that specifies the bean property to be extracted.

  • String: It is a name of the property value that should be set for Mapped property.

  • String: It defines the key of the property value to be set.

  • Object: It specifies the value of property to be set.

Example

The below example demonstrates use of above properties in the beanUtils:

import org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.List;

public class BeanUtilsPropertyDemo{
   public static void main(String args[]){

   try{
      // Creating the bean and allows to access getter and setter properties
      MyBean myBean = new MyBean();

      // Setting the properties on the myBean
      PropertyUtils.setSimpleProperty(myBean, "stringProp", "Hello!This is a string");
      PropertyUtils.setSimpleProperty(myBean, "floatProp", new Float(25.20));

      // Getting the simple properties
      System.out.println("String Property: " + PropertyUtils.getSimpleProperty(myBean, "stringProp"));

      System.out.println("Float Property: " + PropertyUtils.getSimpleProperty(myBean, "floatProp"));

      // Here we will create a list for the indexed property
      List list = new ArrayList();
      list.add("String value 0");
      list.add("String value 1");

      myBean.setListProp(list);

      // get and set this indexed property
      PropertyUtils.setIndexedProperty(myBean, "listProp[1]", "This is new string value 1");

      System.out.println("List Property[1]: " + PropertyUtils.getIndexedProperty(myBean, "listProp[1]"));

   }catch(Exception e){
      System.out.println(e);
   }
   }
}

Now we will create one more class called MyBean.java for the bean class:

import java.util.ArrayList;
import java.util.List;

public class MyBean {
   private String stringProp;
   private float floatProp;

   //indexed property
   @SuppressWarnings("rawtypes")
   private List listProp = new ArrayList();

   public void setStringProp(String stringProp) { this.stringProp = stringProp; }
   public String getStringProp() { return this.stringProp; }

   public void setFloatProp(float floatProp) { this.floatProp = floatProp; }
   public float getFloatProp() { return this.floatProp; }

   public void setListProp(List<?> listProp) { this.listProp = listProp; }
   public List<?&gt getListProp() { return this.listProp; }
	}

Output

Let's carry out the following steps to see how above code works:

  • Save the above first code as BeanUtilsPropertyDemo.java.

  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Basic Property Access
Advertisements