Jakarta commons-BeanUtils

概要

  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 を入手してクラスパスに加えます。
  • Strutsを使っている場合にはこれらのライブラリは同梱されているので特にダウンロードしてくる必要はありません。

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

  1. 次のようなJavaBeansの内部変数を読み書きします
    Everything is expanded. Everything is shortened.
      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;
             }
     }
  2. BeanUtils?を使った読み書き
    Everything is expanded. Everything is shortened.
      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();
             }
         }
     }
  3. 実行結果
    taro
    30
    male

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

  1. ネストしたJavaBeansは、"."区切りで読み書きが可能です
  2. 配列の場合には ${変数名}[0].${変数名} で読み書きが可能です
    Everything is expanded. Everything is shortened.
      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();
             }
         }
     }
  3. 実行結果
    hanako
    miyuki
    tomoko

JavaBeansをMapに変換する

  1. PropertyUtils?.describe(Object) で JavaBeansをMapに変換することが出来ます
    Everything is expanded. Everything is shortened.
      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();
         } 
         }
     }
  2. 実行結果
    {sex=male, age=10, class=class com.snail.HumanInfoBean, name=jiro, children=null}

MapもJavaBeansと同様に扱えます

  1. BeanUtils?を使ったMapの読み書き
    Everything is expanded. Everything is shortened.
      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();
         } 
         }
     }
  2. 実行結果
    taro
    30
    male

JavaBeansのメソッドの実行

  1. MethodUtils?を使うとメソッドを実行することが出来ます
    Everything is expanded. Everything is shortened.
      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();
             }
         }
     }
  2. 実行結果
    Hello World
  3. (補足)デフォルトコンストラクタ以外を使ってクラスをオブジェクト化する
    Everything is expanded. Everything is shortened.
      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/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package at java.lang.ClassLoader.checkCerts(Unknown Source) at java.lang.ClassLoader.preDefineClass(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1817) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1325) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at org.hibernate.mapping.Table.(Table.java:32) at org.hibernate.cfg.Mappings.addTable(Mappings.java:120) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:251) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:236) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:152) at org.hibernate.cfg.Configuration.add(Configuration.java:362) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:400) at org.hibernate.cfg.Configuration.addResource(Configuration.java:449) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184) at org.hibernate.cfg.Configuration.configure(Configuration.java:1112) at com.chinafi.hibernate.SessionFactory.(SessionFactory.java:30) at com.chinafi.hibernate.BaseD

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值