Commons BeanUtils – 初期設定

1月 1, 2003 · Posted in Apache Commons · Comment 

Commons BeanUtilsとは

JavaBeansに対する汎用的なメソッド群を提供している。開発者がリフレクションなどの機能を実装することなく、プロパティへの動的アクセスが実現されます。

ダウンロード〜設定

  1. Commons BeanutilsのWebサイトにアクセス
  2. バイナリファイルをダウンロードする。
  3. 解凍
  4. commons-beanutils.jar, commons-beanutils-core.jar, commons-beanutils-bean-collections.jarを任意の位置に保存
  5. パスを通す

Commons BeanUtils – PropertyUtils

1月 1, 2003 · Posted in Apache Commons · Comment 

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の相互変換などに便利。

Commons Digester – 初期設定

1月 1, 2003 · Posted in Apache Commons · Comment 

Commons Digesterとは

XMLファイルを読み込み、指定されたルールに基づいてJavaのオブジェクトを生成するためのフレームワーク。
Strutsの一部から独立した。

ダウンロード〜設定

オフィシャルサイトからバイナリを入手する。

オフィシャルサイトにもあるように、DigesterはCommonsのLogging、BeanUtils、Collectionsに依存しているため、別途バイナリの入手が必要。

次ページへ »