Java BeanUtils - 嵌套的属性访问

描述

您可以通过使用"."连接访问路径的属性名来访问bean的嵌套属性的值.分隔符.

您可以使用以下方法获取和设置嵌套属性的值:

  • PropertyUtils.getNestedProperty(Object,String)

  • PropertyUtils.setNestedProperty(对象,字符串,对象)

参数:

  • 对象:它是一个要获取或修改其属性的bean.

  • 字符串:它是要获取或修改的嵌套属性的名称.

示例

在此示例中,您将了解如何获取和设置嵌套属性的值.我们将创建三个类; bean的 SubBean AppLayer1Bean BeanUtilsDemo 作为要运行的主程序.

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);
        }
    }
}

现在我们将创建另一个名为 SubBean.java的类如下所示:

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; 
    }
}

创建另一个类 AppLayer1Bean.java 以及下面的代码:

public class AppLayer1Bean {
    private SubBean subBean;

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

输出

让我们执行以下步骤看看上面的代码是如何工作的:

  • 将上面的第一个代码保存为 BeanUtilsDemo.java .

  • 现在使用"运行"选项或Ctrl + f11执行代码并显示如下输出.

嵌套属性访问

PropertyUtils方法签名

PropertyUtils 类提供以下方法,它接受简单,索引和映射属性访问的任意组合以获取和设置值指定bean的属性.

  • PropertyUtils.getProperty(Object,String)

  • PropertyUtils.setProperty(Object,String,Object)

参数:

  • 对象:它是一个要获取或修改其属性的bean.

  • String :它是要获取或修改的索引和/或嵌套属性的名称.

示例

以下简单程序说明了使用getProperty和setProperty方法:

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;
        }
    }
}

运行上例中指定的代码,你会得到以下输出:

嵌套属性访问