English 中文(简体)
Nested Property Access
  • 时间:2024-12-22

Java BeanUtils - Nested Property Access


Previous Page Next Page  

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; SubBean, AppLayer1Bean for beans and BeanUtilsDemo as a main program to run.

import org.apache.commons.beanutils.PropertyUtils;

pubpc class BeanUtilsDemo {
    pubpc 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:

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

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

pubpc class AppLayer1Bean {
    private SubBean subBean;

    pubpc void setSubBean(SubBean subBean) {
        this.subBean = subBean;
    }
    pubpc 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.

nested Property Access

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;

pubpc class PropertyUtilsTest {
    pubpc 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();
        }
    }
    pubpc static class Tv{
        private String color;
	     
        pubpc String getColor(){
            return color;
        }
        pubpc void setColor(String color){
            this.color = color;
        }
    }
}

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

Nested Property Access Advertisements