Java BeanUtils – Nested Property Access

Java BeanUtils - Nested Property Access

This topic is about Java BeanUtils – Nested Property Access.

Description

You can access the value of nested property of the bean by concatenating the property names of the access path by using “.” separators.

You can get and set the values of Nested property by using the below methods:

  • PropertyUtils.getNestedProperty(Object, String)
  • PropertyUtils.setNestedProperty(Object, String, Object)

Parameters:

  • Object: It is a bean whose property to be obtained or modified.
  • String: It is a name of the nested property to be obtained or modified.

Example

In this example, you’ll see how to get and set the values of nested property. We will be creating three classes; SubBeanAppLayer1Bean for beans and BeanUtilsDemo as a main program to run.

import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilsDemo {
    public static void main(String args[]){
        try{
            // create the bean
            AppLayer1Bean nested = new AppLayer1Bean();
            // set a SubBean which is part of another bean
            SubBean sb = new SubBean();
            sb.setStringProperty("Hello World from SubBean");
            nested.setSubBean(sb);
		
            // accessing and setting nested properties
            PropertyUtils.setNestedProperty(
                nested, "subBean.stringProperty",
                "Hello World from SubBean, set via Nested Property Access");
			
            System.out.println(
                PropertyUtils.getNestedProperty(nested, "subBean.stringProperty"));
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}

Now we will create another class called SubBean.java as shown below:

public class SubBean {
    private int intProperty;
    private String stringProperty;
	
    public void setIntProperty(int intProperty) { 
        this.intProperty = intProperty; 
    }
    public int getIntProperty() {
        return this.intProperty; 
    }
	
    public void setStringProperty(String stringProperty) { 
        this.stringProperty = stringProperty; 
    }
    public String getStringProperty() { 
        return this.stringProperty; 
    }
}

Create the one more class AppLayer1Bean.java along with the below code:

public class AppLayer1Bean {
    private SubBean subBean;

    public void setSubBean(SubBean subBean) {
        this.subBean = subBean;
    }
    public SubBean getSubBean(){
        return this.subBean;
    }	
}

Output

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

  • Save the above first code as BeanUtilsDemo.java.
  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

PropertyUtils Method Signatures

The following methods are provided by the PropertyUtils class, which accepts any arbitrary combinations of simple, indexed and mapped property access to get and set the value of the property of the specified bean.

  • PropertyUtils.getProperty(Object, String)
  • PropertyUtils.setProperty(Object, String, Object)

Parameters:

  • Object: It is a bean whose property to be obtained or modified.
  • String: It is a name of the indexed and/or nested property to be obtained or modified.

Example

The following simple program illustrates the use of getProperty and setProperty methods:

import org.apache.commons.beanutils.PropertyUtils;

public class PropertyUtilsTest {
    public static void main(String args[]){
        try{
            Tv Color = new Tv();
            PropertyUtils.setProperty(Color, "color", "Black");
            String value = (String) PropertyUtils.getProperty(Color, "color");
            System.out.println("The color value of Tv is: " + value);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public static class Tv{
        private String color;
	     
        public String getColor(){
            return color;
        }
        public void setColor(String color){
            this.color = color;
        }
    }
}

Run the code as specified in the above example and you would get the below output:

In this topic we learned Java BeanUtils – Nested Property Access. To know more, Click Here.

This Post Has One Comment

Leave a Reply