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

          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開源團隊 閱讀(767) 評論(1)  編輯  收藏

          評論

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


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


          網站導航:
           
          主站蜘蛛池模板: 通海县| 九龙城区| 丽水市| 富源县| 绥阳县| 城口县| 康保县| 龙州县| 泗水县| 文山县| 台东市| 康平县| 永顺县| 中江县| 宁明县| 佛冈县| 巴中市| 万盛区| 霍邱县| 新竹市| 师宗县| 施甸县| 凤庆县| 萍乡市| 黄浦区| 土默特右旗| 马尔康县| 临清市| 太湖县| 区。| 广德县| 丹棱县| 鲜城| 十堰市| 涿州市| 醴陵市| 噶尔县| 阿拉尔市| 孝义市| 庐江县| 孟津县|