Java BeanUtils - Nested Property Access

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.getNestedProperty(Object, String)
PropertyUtils.setNestedProperty(Object, String, Object)
PropertyUtils.setNestedProperty(Object, String, Object)
Parameters:
Object: It is a bean whose property to be obtained or modified.
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.
String: It is a name of the nested property to be obtained or modified.
In this example, you'll see how to get and set the values of nested property. We will be creating three classes; SubBean, AppLayer1Bean 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;
    }	
}
Let's carry out the following steps to see how above code works:
Save the above first code as BeanUtilsDemo.java.
Save the above first code as BeanUtilsDemo.java.
Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.
Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.
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.getProperty(Object, String)
PropertyUtils.setProperty(Object, String, Object)
PropertyUtils.setProperty(Object, String, Object)
Parameters:
Object: It is a bean whose property to be obtained or modified.
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.
String: It is a name of the indexed and/or nested property to be obtained or modified.
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:

    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page