如鵬網 大學生計算機學習社區

          CowNew開源團隊

          http://www.cownew.com 郵件請聯系 about521 at 163.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            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提供了調用構造函數的方法,使用public static Object invokeConstructor(Class klass, Object arg)可以直接調用某個類的構造函數。
          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開源團隊 閱讀(768) 評論(1)  編輯  收藏

          評論

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


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


          網站導航:
           
          主站蜘蛛池模板: 平凉市| 东台市| 于都县| 嘉荫县| 桦南县| 蓬安县| 稷山县| 颍上县| 禹城市| 宾川县| 平谷区| 清水河县| 梁河县| 察隅县| 定边县| 马龙县| 原阳县| 保靖县| 长汀县| 南陵县| 手游| 自贡市| 金川县| 太仓市| 景东| 许昌县| 庆安县| 合水县| 杭州市| 南华县| 常熟市| 青神县| 黔东| 沂源县| 永福县| 永德县| 临洮县| 定兴县| 长兴县| 集安市| 九江县|