目录

嵌套属性访问(Nested Property Access)

描述 (Description)

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

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

  • PropertyUtils.getNestedProperty(Object, String)

  • PropertyUtils.setNestedProperty(Object, String, Object)

参数:

  • Object :它是一个要获取或修改其属性的bean。

  • String :它是要获取或修改的嵌套属性的名称。

例子 (Example)

在此示例中,您将了解如何获取和设置嵌套属性的值。 我们将创建三个类; 用于bean的SubBeanAppLayer1Bean和作为运行的主程序的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;
    }	
}

输出 (Output)

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

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

  • 现在使用Run选项或Ctrl + f11执行代码,并显示如下输出。

嵌套的Property Access

PropertyUtils方法签名

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

  • PropertyUtils.getProperty(Object, String)

  • PropertyUtils.setProperty(Object, String, Object)

参数:

  • Object :它是一个要获取或修改其属性的bean。

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

例子 (Example)

以下简单程序说明了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;
        }
    }
}

运行上面示例中指定的代码,您将得到以下输出:

嵌套属性访问
↑回到顶部↑
WIKI教程 @2018