Java-beanutils-standard-javabeans-basic-property-access
提供:Dev Guides
Java BeanUtils-基本的なプロパティアクセス
説明
次の方法を使用して、基本プロパティにアクセスできます。
- シンプルなプロパティ
- インデックス付きプロパティ
- マッピングされたプロパティ
シンプルなプロパティ
以下のAPIシグネチャを使用して、 simple プロパティ値を取得および設定できます。
- PropertyUtils.getSimpleProperty(Object、String)
- PropertyUtils.SetSimpleProperty(Object、String、Object)
パラメーター:
- オブジェクト:抽出されるBeanプロパティを指定するBeanオブジェクトです。
- 文字列:抽出するプロパティの名前を指定する文字列名です。
インデックス付きプロパティ
*indexed* プロパティを作成するには、2つのオプションを使用できます。最初のオプションは添え字をプロパティ名に構築し、2番目のオプションはメソッドを呼び出す別の引数で添え字を定義します。
インデックス付きプロパティは、以下のメソッドを使用して取得および設定できます。
- PropertyUtils.getIndexedProperty(Object、String)
- PropertyUtils.getIndexedProperty(Object、String、int)
- PropertyUtils.setIndexedProperty(Object、String、Object)
- PropertyUtils.setIndexedProperty(Object、String、int、Object)
パラメーター:
- オブジェクト:抽出されるBeanプロパティを指定するBeanオブジェクトです。
- 文字列:抽出するプロパティの名前を指定する文字列名です。
- int :プロパティ値のインデックスを設定します。
- オブジェクト:インデックス付きプロパティ要素の値を指定します。
マッピングされたプロパティ
以下のAPIシグネチャを使用して、 mapped プロパティ値を取得および設定できます。 追加の引数がある場合は、角括弧を使用する代わりに、括弧内に( "(" and ")")として記述することができます。
- PropertyUtils.getMappedProperty(Object、String)
- PropertyUtils.getMappedProperty(Object、String、String)
- PropertyUtils.setMappedProperty(Object、String、Object)
- PropertyUtils.setMappedProperty(Object、String、String、Object)
パラメーター:
- オブジェクト:抽出されるBeanプロパティを指定するBeanオブジェクトです。
- 文字列:マップされたプロパティに設定するプロパティ値の名前です。
- 文字列:設定するプロパティ値のキーを定義します。
- オブジェクト:設定するプロパティの値を指定します。
例
以下の例は、beanUtilsで上記のプロパティを使用する方法を示しています。
import org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.List;
public class BeanUtilsPropertyDemo{
public static void main(String args[]){
try{
//Creating the bean and allows to access getter and setter properties
MyBean myBean = new MyBean();
//Setting the properties on the myBean
PropertyUtils.setSimpleProperty(myBean, "stringProp", "Hello!This is a string");
PropertyUtils.setSimpleProperty(myBean, "floatProp", new Float(25.20));
//Getting the simple properties
System.out.println("String Property: " + PropertyUtils.getSimpleProperty(myBean, "stringProp"));
System.out.println("Float Property: " + PropertyUtils.getSimpleProperty(myBean, "floatProp"));
//Here we will create a list for the indexed property
List list = new ArrayList();
list.add("String value 0");
list.add("String value 1");
myBean.setListProp(list);
//get and set this indexed property
PropertyUtils.setIndexedProperty(myBean, "listProp[1]", "This is new string value 1");
System.out.println("List Property[1]: " + PropertyUtils.getIndexedProperty(myBean, "listProp[1]"));
}catch(Exception e){
System.out.println(e);
}
}
}
次に、Beanクラス用に_MyBean.java_というクラスをもう1つ作成します。
import java.util.ArrayList;
import java.util.List;
public class MyBean {
private String stringProp;
private float floatProp;
//indexed property
@SuppressWarnings("rawtypes")
private List listProp = new ArrayList();
public void setStringProp(String stringProp) { this.stringProp = stringProp; }
public String getStringProp() { return this.stringProp; }
public void setFloatProp(float floatProp) { this.floatProp = floatProp; }
public float getFloatProp() { return this.floatProp; }
public void setListProp(List<?> listProp) { this.listProp = listProp; }
public List<?> getListProp() { return this.listProp; }
}
出力
上記のコードがどのように機能するかを確認するために、次の手順を実行しましょう。
- 上記の最初のコードを_BeanUtilsPropertyDemo.java_として保存します。
- [実行]オプションまたはCtrl + f11を使用してコードを実行すると、次のような出力が表示されます。