概要

  1. JavaBeansをMapの様に文字列を使って内部変数の出し入れを出来るようにするものです
  2. Strutsの内部で使われていたものが単独のプロジェクトとしてスピンアウトしたものです

インストール

  1. ダウンロードサイトから
    commons-beanutils-1.7.0.zip
    をダウンロードします
  2. 展開して出来た
    commons-beanutils.jar
    commons-beanutils-core.jar
    commons-beanutils-bean-collections.jar
    をクラスパスに加えます
  3. 内部的にJakarta-Commons-Logging?を使っているので、commons-logging.jar を入手してクラスパスに加えます。

JavaBeans内部変数の読み取り/書き取り

  1. 次のようなJavaBeansの内部変数を読み書きします
     package com.snail;
     
     public class HumanInfoBean {
     
     	private String name;
     
     	private String age;
     
     	private String sex;
     
     	private HumanInfoBean[] children;
     
     	public HumanInfoBean() {
     		super();
     	}
     
     	public String getAge() {
     		return age;
     	}
     
     	public void setAge(String age) {
     		this.age = age;
     	}
     
     	public HumanInfoBean[] getChildren() {
     		return children;
     	}
     
     	public void setChildren(HumanInfoBean[] children) {
     		this.children = children;
     	}
     
     	public String getName() {
     		return name;
     	}
     
     	public void setName(String name) {
     		this.name = name;
     	}
     
     	public String getSex() {
     		return sex;
     	}
     
     	public void setSex(String sex) {
     		this.sex = sex;
     	}
             
             public String sayHello( String str ){
                       return "Hello " + str;
             }
     }
    
  2. BeanUtils?を使った読み書き
     package com.snail;
     
     import org.apache.commons.beanutils.PropertyUtils;
     
     import java.lang.reflect.InvocationTargetException;
     
     
     public class BeanUtilExam {
         private BeanUtilExam() {
             super();
         }
     
         public static void main(String[] args) {
             HumanInfoBean parent = new HumanInfoBean();
     
             try {
                 PropertyUtils.setProperty(parent, "name", "taro");
                 PropertyUtils.setProperty(parent, "age", "30");
                 PropertyUtils.setProperty(parent, "sex", "male");
     
                 System.out.println(PropertyUtils.getProperty(parent, "name"));
                 System.out.println(PropertyUtils.getProperty(parent, "age"));
                 System.out.println(PropertyUtils.getProperty(parent, "sex"));
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             } catch (InvocationTargetException e) {
                 e.printStackTrace();
             } catch (NoSuchMethodException e) {
                 e.printStackTrace();
             }
         }
     }
    
  3. 実行結果
    taro
    30
    male

ネストしたJavaBeans内部変数の読み取り/書き取り

  1. ネストしたJavaBeansは、"."区切りで読み書きが可能です
  2. 配列の場合には ${変数名}[0].${変数名} で読み書きが可能です
     package com.snail;
     
     import org.apache.commons.beanutils.PropertyUtils;
     
     import java.lang.reflect.InvocationTargetException;
     
     
     public class BeanUtilExam {
         private BeanUtilExam() {
             super();
         }
     
         /**
          * @param args
          */
         public static void main(String[] args) {
             HumanInfoBean parent = new HumanInfoBean();
             HumanInfoBean child1 = new HumanInfoBean();
             HumanInfoBean child2 = new HumanInfoBean();
             HumanInfoBean child3 = new HumanInfoBean();
     
             parent.setChildren(new HumanInfoBean[] { child1, child2, child3 });
     
             try {
                 PropertyUtils.setProperty(parent, "children[0].name", "hanako");
                 PropertyUtils.setProperty(parent, "children[1].name", "miyuki");
                 PropertyUtils.setProperty(parent, "children[2].name", "tomoko");
     
                 System.out.println(PropertyUtils.getProperty(parent,"children[0].name"));
                 System.out.println(PropertyUtils.getProperty(parent,"children[1].name"));
                 System.out.println(PropertyUtils.getProperty(parent,"children[2].name"));
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             } catch (InvocationTargetException e) {
                 e.printStackTrace();
             } catch (NoSuchMethodException e) {
                 e.printStackTrace();
             }
         }
     }
    
  3. 実行結果
    hanako
    miyuki
    tomoko

JavaBeansをMapに変換する

  1. PropertyUtils?.describe(Object) で JavaBeansをMapに変換することが出来ます
     package com.snail;
     
     import java.lang.reflect.InvocationTargetException;
     import java.util.Map;
     
     import org.apache.commons.beanutils.PropertyUtils;
     
     
     public class BeanUtilExam {
         private BeanUtilExam() {
             super();
         }
     
         public static void main(String[] args) {
         	HumanInfoBean human = new HumanInfoBean();
         	
         	human.setAge("10");
         	human.setName("jiro");
         	human.setSex("male");
         	
         	try {
         		Map map = PropertyUtils.describe(human);
     		System.out.println(map);
     	} catch (IllegalAccessException e) {
     		e.printStackTrace();
     	} catch (InvocationTargetException e) {
     		e.printStackTrace();
     	} catch (NoSuchMethodException e) {
     		e.printStackTrace();
     	} 
         }
     }
    
  2. 実行結果
    {sex=male, age=10, class=class com.snail.HumanInfoBean, name=jiro, children=null}

MapもJavaBeansと同様に扱えます

  1. BeanUtils?を使ったMapの読み書き
     package com.snail;
     
     import java.lang.reflect.InvocationTargetException;
     import java.util.HashMap;
     import java.util.Map;
     
     import org.apache.commons.beanutils.PropertyUtils;
     
     
     public class BeanUtilExam {
         private BeanUtilExam() {
             super();
         }
     
         public static void main(String[] args) {
         	Map<String,String> parent = new HashMap<String,String>();
         	
         	try {
                 PropertyUtils.setProperty(parent, "name", "taro");
                 PropertyUtils.setProperty(parent, "age", "30");
                 PropertyUtils.setProperty(parent, "sex", "male");
     
                 System.out.println(PropertyUtils.getProperty(parent, "name"));
                 System.out.println(PropertyUtils.getProperty(parent, "age"));
                 System.out.println(PropertyUtils.getProperty(parent, "sex"));
     
     	} catch (IllegalAccessException e) {
     		e.printStackTrace();
     	} catch (InvocationTargetException e) {
     		e.printStackTrace();
     	} catch (NoSuchMethodException e) {
     		e.printStackTrace();
     	} 
         }
     }
    
  2. 実行結果
    taro
    30
    male

JavaBeansのメソッドの実行

  1. MethodUtils?を使うとメソッドを実行することが出来ます
     package com.snail;
     
     import java.lang.reflect.InvocationTargetException;
     
     import org.apache.commons.beanutils.MethodUtils;
     
     
     public class BeanUtilExam {
         private BeanUtilExam() {
             super();
         }
     
         /**
          * @param args
          */
         public static void main(String[] args) {
             try {
                 Object parent = Class.forName("com.snail.HumanInfoBean").newInstance();
                 
                 System.out.println(MethodUtils.invokeMethod(parent, "sayHello",
                         new Object[] { "World" }, new Class[] { String.class }));
             } catch (NoSuchMethodException e) {
                 e.printStackTrace();
             } catch (IllegalAccessException e) {
                 e.printStackTrace();
             } catch (InvocationTargetException e) {
                 e.printStackTrace();
             } catch (InstantiationException e) {
                 e.printStackTrace();
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
             }
         }
     }
    
  2. 実行結果
    Hello World
  3. (補足)デフォルトコンストラクタ以外を使ってクラスをオブジェクト化する
     Class class = Class.forName( "${クラス名}" );
     Constructor constructor = class.getConstructor( new Class[]{ String.class , Integer.class } );
     Object object = constructor.newInstance("ALPHA",1);
    

参考文献

http://jakarta.apache.org/commons/beanutils/


Java#Jakarta


トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2006-04-15 (土) 23:59:19 (6578d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=40742b9b94
ISBN10
ISBN13
9784061426061