如鵬網(wǎng) 大學生計算機學習社區(qū)

          CowNew開源團隊

          http://www.cownew.com 郵件請聯(lián)系 about521 at 163.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            363 隨筆 :: 2 文章 :: 808 評論 :: 0 Trackbacks
          BeanUtils
          BeanUtils是Apache-Commons項目提供的另一個非常方便的類庫,通過這個類庫能夠更方便的使用反射。最常用的類是BeanUtils(org.apache.commons.beanutils包中),使用這個類能通過名字訪問一個Bean中的某個屬性。
          通過BeanUtils.getProperty(person,”age”)能得到person的age屬性。此方法還支持內嵌對象,比如BeanUtils.getProperty(person,”manager.name”)就能得到person的manager屬性的name屬性。還支持List和Map類型的屬性,如下面的語法即可取得Order的顧客列表中第一個顧客的名字BeanUtils.getProperty(orderBean, "customers[1].name")。?使用BeanUtils.setProperty方法則可以設置javaBean的屬性值。
          ConstructorUtils提供了調用構造函數(shù)的方法,使用public static Object invokeConstructor(Class klass, Object arg)可以直接調用某個類的構造函數(shù)。
          MethodUtils提供了調用bean方法的方法,使用MethodUtils.invokeMethod(bean, methodName, parameter);可以直接調用某個類的某個方法。
          PropertyUtils提供了更詳細的屬性訪問方法,使用public static Class getPropertyType(Object bean, String name)獲取屬性的Class類型。
          UserInfo userInfo = (UserInfo) ConstructorUtils.invokeConstructor(
          ????UserInfo.class, new Object[] {});
          PersonInfo personInfo = (PersonInfo) ConstructorUtils
          ????.invokeConstructor(PersonInfo.class, new Object[] {});
          BeanUtils.setProperty(personInfo, "age", new Integer(20));
          BeanUtils.setProperty(personInfo, "name", "Tom");
          BeanUtils.setProperty(userInfo, "number", "admin");
          ??BeanUtils.setProperty(userInfo, "person", personInfo);
          System.out.println(BeanUtils.getProperty(userInfo, "person.name"));
          BeanUtils.setProperty(userInfo, "person.name","xdx");
          System.out.println(BeanUtils.getProperty(userInfo, "person.name"));
          System.out.println(PropertyUtils.getPropertyType(userInfo,"person"));
          運行結果:
          Tom
          xdx
          class com.cownew.PIS.basedata.common.PersonInfo
          posted on 2007-02-05 12:49 CowNew開源團隊 閱讀(771) 評論(1)  編輯  收藏

          評論

          # re: xjl:BeanUtils 2007-02-09 14:00 壞男孩
          ...................★
          ..................▍..★
          ..................▍.一 .☆
          ................. ▍ ..帆. ★
          ..................▍ ... 風. ☆
          ..................▍ ... ..順. ★
          ................. ▍.萬 事 如 意. ☆
          ..................▍☆ .★ .☆ .★. ☆
          ..................▍
           ..▍∵ ☆ ★...▍▍....█▍ ☆ ★∵▍..
             ◥█▅▅██▅▅██▅▅▅▅▅███◤
             .◥███████████████◤
           ~~~~◥█████████████◤~~~~
          ~~~~~~~~~~~~~~~~~~~~~~~~~  回復  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 阿坝| 石渠县| 临湘市| 微山县| 枣阳市| 塔城市| 定陶县| 海淀区| 西充县| 延吉市| 南投县| 公主岭市| 韶山市| 紫云| 普陀区| 景德镇市| 乌恰县| 哈巴河县| 江都市| 靖江市| 原平市| 玉田县| 三都| 兴义市| 长顺县| 广州市| 滨海县| 长海县| 扶风县| 萝北县| 纳雍县| 临安市| 乐都县| 射阳县| 鹤山市| 景东| 佛山市| 潞西市| 藁城市| 汤原县| 巧家县|