概要 †
- JavaBeansをMapの様に文字列を使って内部変数の出し入れを出来るようにするものです
- Strutsの内部で使われていたものが単独のプロジェクトとしてスピンアウトしたものです
インストール †
- ダウンロードサイトから
commons-beanutils-1.7.0.zip
をダウンロードします - 展開して出来た
commons-beanutils.jar commons-beanutils-core.jar commons-beanutils-bean-collections.jar
をクラスパスに加えます - 内部的にJakarta-Commons-Logging?を使っているので、commons-logging.jar を入手してクラスパスに加えます。
- Strutsを使っている場合にはこれらのライブラリは同梱されているので特にダウンロードしてくる必要はありません。
JavaBeans内部変数の読み取り/書き取り †
- 次のようなJavaBeansの内部変数を読み書きします
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
- | | | | | | | | | - | ! | - | ! | - | ! | - | ! | - | ! | - | ! | - | ! | - | ! | - | ! | - | ! !
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; } }
- BeanUtils?を使った読み書き
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
- - | ! | - | | - | | | | | | | - | - | - | ! ! !
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(); } } }
- 実行結果
taro 30 male
ネストしたJavaBeans内部変数の読み取り/書き取り †
- ネストしたJavaBeansは、"."区切りで読み書きが可能です
- 配列の場合には ${変数名}[0].${変数名} で読み書きが可能です
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
- - | ! | | | | - | | | | | | | - | | | | | | | - | - | - | ! ! !
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(); } } }
- 実行結果
hanako miyuki tomoko
JavaBeansをMapに変換する †
- PropertyUtils?.describe(Object) で JavaBeansをMapに変換することが出来ます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
- - | ! | - | | | | | | - | | - | - | - | ! ! !
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(); } } }
- 実行結果
{sex=male, age=10, class=class com.snail.HumanInfoBean, name=jiro, children=null}
MapもJavaBeansと同様に扱えます †
- BeanUtils?を使ったMapの読み書き
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
- - | ! | - | | - | | | | | | | | - | - | - | ! ! !
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(); } } }
- 実行結果
taro 30 male
JavaBeansのメソッドの実行 †
- MethodUtils?を使うとメソッドを実行することが出来ます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
- - | ! | | | | - - | | | | - | - | - | - | - | ! ! !
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(); } } }
- 実行結果
Hello World
- (補足)デフォルトコンストラクタ以外を使ってクラスをオブジェクト化する
1 2 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/