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

          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 壞男孩
          ...................★
          ..................▍..★
          ..................▍.一 .☆
          ................. ▍ ..帆. ★
          ..................▍ ... 風. ☆
          ..................▍ ... ..順. ★
          ................. ▍.萬 事 如 意. ☆
          ..................▍☆ .★ .☆ .★. ☆
          ..................▍
           ..▍∵ ☆ ★...▍▍....█▍ ☆ ★∵▍..
             ◥█▅▅██▅▅██▅▅▅▅▅███◤
             .◥███████████████◤
           ~~~~◥█████████████◤~~~~
          ~~~~~~~~~~~~~~~~~~~~~~~~~  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 榕江县| 康平县| 宜丰县| 当阳市| 温州市| 宝清县| 海丰县| 通州区| 铜陵市| 高邮市| 安宁市| 德阳市| 兴安县| 凤凰县| 成都市| 赤壁市| 瑞昌市| 拜泉县| 曲周县| 遂昌县| 盐城市| 吕梁市| 白水县| 英山县| 开鲁县| 喜德县| 弋阳县| 临西县| 大方县| 长丰县| 道孚县| 德化县| 太白县| 克东县| 淮滨县| 周至县| 贵德县| 莲花县| 德清县| 白城市| 淮北市|