Java BeanUtils - 巢狀屬性訪問



描述

您可以透過使用“.”分隔符連線訪問路徑的屬性名稱來訪問 Bean 的巢狀屬性的值。

您可以使用以下方法獲取和設定巢狀屬性的值

  • PropertyUtils.getNestedProperty(Object, String)

  • PropertyUtils.setNestedProperty(Object, String, Object)

引數

  • Object:它是要獲取或修改其屬性的 Bean。

  • String:是要獲取或修改的巢狀屬性的名稱。

示例

在這個示例中,您將看到如何獲取和設定巢狀屬性的值。我們將建立三個類;SubBeanAppLayer1Bean 作為 Bean,以及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 執行程式碼,如下所示的輸出將顯示。

nested Property Access

PropertyUtils 方法簽名

PropertyUtils 類提供了以下方法,這些方法接受任意組合的簡單、索引和對映屬性訪問,以獲取和設定指定 Bean 的屬性的值。

  • PropertyUtils.getProperty(Object, String)

  • PropertyUtils.setProperty(Object, String, Object)

引數

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

按照上述示例中的說明執行程式碼,您將獲得以下輸出

Nested Property Access
廣告

© . All rights reserved.