@634

Commons BeanUtils - PropertyUtils

Advertisement

PropertyUtilsクラス

org.apache.commons.beanutils.PropertyUtilsクラスはリフレクションAPIを用いて実装されていて、JavaBesnsのプロパティに対する汎用的なアクセスを提供する。

プロパティの読み書き

PropertyUtilsを用いてBeanプロパティの読み書きを行うと、プロパティファイルに読み書きを行うようなイメージで行うことができる。

java.lang.String型のプロパティnameとint型のプロパティageがあり、それに対するsetter/getterメソッドのみを保持しているBeanを利用する。
import java.io.Serializable;

public class Employee implements Serializable{
    private String name;
    private int age;

    public Employee(){
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        this.age = age;
    }
}

読み書きの実行
BeanUtilSample.java
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilSample {
    public static void main(String[] args) throws Exception{
        // 初期化
        Profile profile = new Profile();
        profile.setName("tarou");
        profile.setAge(10);

        // 値の取得
        String name = (String)PropertyUtils.getProperty(profile, "name");
        Integer age = (Integer)PropertyUtils.getProperty(profile, "age");
        System.out.println(name);
        System.out.println(age);

        // 値の設定
        PropertyUtils.setProperty(profile, "name", new String("jirou"));
        PropertyUtils.setProperty(profile, "age", new Integer("20"));

        // 値の取得
        System.out.println(PropertyUtils.getProperty(profile, "name"));
        System.out.println(PropertyUtils.getProperty(profile, "age"));
	}
}

結果
tarou
10
jirou
20
getPropertyでプロパティの取得、setPropertyでプロパティの設定を行っている。基本データ型のプロパティは、ラッパークラスに自動変換される。

配列の利用

配列のプロパティを保持しているクラスを定義する。
Names.java
public class Names {
    private String[] name;

    public String[] getName() {
        return name;
    }

    public void setName(String[] name) {
        this.name = name;
    }
}

配列の各項目に対してindex番号を用いた読み書きを行うことができる。
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilSample3 {
    public static void main(String[] args) throws Exception{
        // 初期化
        Names names = new Names();
        names.setName(new String[2]);

        // 値の設定
        PropertyUtils.setIndexedProperty(names, "name", 0, "tarou");
        PropertyUtils.setIndexedProperty(names, "name", 1, "jirou");

        // 値の取得
        System.out.println(PropertyUtils.getIndexedProperty(names, "name", 0));
        System.out.println(PropertyUtils.getIndexedProperty(names, "name", 1));
    }
}

プロパティのコピー

新たなBeanクラスを定義
Profile.java
import java.io.Serializable;

public class Profile implements Serializable{
    private String name;

    public Profile(){
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

上記で定義したEmployeeクラスのプロパティをProfileクラスにコピーする。
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilSample {
    public static void main(String[] args) throws Exception{
        Profile profile = new Profile();
        profile.setName("tarou");

        Employee emp = new Employee();

        PropertyUtils.copyProperties(emp, profile);

        System.out.println((String)PropertyUtils.getProperty(emp, "name"));
    }
}
同じ名称のプロパティが自動的にコピーされる。DTOとBeanの相互変換などに便利。

Advertisement

ショートカット

634
このカテゴリのトップページに戻る
634labs
   UIコレクションギャラリー

サイト検索

Google

Web サイト内

Y!ログール