隨筆-42  評(píng)論-578  文章-1  trackbacks-0

          commons-beanutils.jar可以到Apache官網(wǎng)的commons子項(xiàng)目下找到它,或者,在Struts2,Spring的下載包中也能看到它的影子。單獨(dú)使用時(shí),要多引進(jìn)一個(gè)commons-logging.jar,這個(gè)包也是非常見的,可在有commons-beanutils.jar的地方找到它。

          好,我們來測(cè)試一下,怎么使用這個(gè)類庫(kù)中的兩個(gè)常用類及它的常用方法。首先,寫一個(gè)POJO類,代碼如下:

          package rong.propertyUtils;

          public class Entity {
              
              
          private Integer id = 5;
              
          private String name = "rongxinhua";
              
              
          public Integer getId() {
                  
          return id;
              }

              
          public void setId(Integer id) {
                  
          this.id = id;
              }

              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
              
          public String haha(){
                  
          return "Ha,Ha";
              }

              
              
          public void sayHelle(String name){
                  System.out.println(name 
          + " say, Hello!");
              }

              
              
          public String countAges(int x, int y){
                  
          return "My Age is " + (x + y);
              }

              
          }


          接著寫一個(gè)測(cè)試的類,代碼如下:

          package rong.propertyUtils;

          import java.util.Map;
          import org.apache.commons.beanutils.MethodUtils;
          import org.apache.commons.beanutils.PropertyUtils;

          public class TestPropertyUtils {
              
              
          public static void main(String[] args) throws Exception{
                  
                  Entity entity 
          = new Entity();
                  
                  
          //通過PropertyUtils的getProperty方法獲取指定屬性的值
                  Integer id = (Integer)PropertyUtils.getProperty(entity, "id");
                  String name 
          = (String)PropertyUtils.getProperty(entity, "name");
                  System.out.println(
          "id = " + id + "  name = " + name);
                  
                  
          //調(diào)用PropertyUtils的setProperty方法設(shè)置entity的指定屬性
                  PropertyUtils.setProperty(entity, "name""心夢(mèng)帆影");
                  System.out.println(
          "name = " + entity.getName());
                  
                  
          //通過PropertyUtils的describe方法把entity的所有屬性與屬性值封裝進(jìn)Map中
                  Map map = PropertyUtils.describe(entity);
                  System.out.println(
          "id = " + map.get("id"+ "  name = " + map.get("name"));
                  
                  
          //通過MethodUtils的invokeMethod方法,執(zhí)行指定的entity中的方法(無參的情況)
                  System.out.println( MethodUtils.invokeMethod(entity, "haha"null) );
                  
                  
          //通過MethodUtils的invokeMethod方法,執(zhí)行指定的entity中的方法(1參的情況)
                  MethodUtils.invokeMethod(entity, "sayHelle""心夢(mèng)帆影");
                  
                  
          //通過MethodUtils的invokeMethod方法,執(zhí)行指定的entity中的方法(多參的情況)
                  Object[] params = new Object[]{new Integer(10),new Integer(12)};
                  String msg 
          = (String)MethodUtils.invokeMethod(entity, "countAges", params);
                  System.out.println(msg);
                  
              }


          }

           

          執(zhí)行結(jié)果如下:

          id = 5  name = rongxinhua
          name 
          = 心夢(mèng)帆影
          id 
          = 5  name = 心夢(mèng)帆影
          Ha
          ,Ha
          心夢(mèng)帆影 say
          , Hello!
          My Age is 
          22


          本文原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處,謝謝!http://www.aygfsteel.com/rongxh7(心夢(mèng)帆影JavaEE技術(shù)博客)
              

          posted on 2009-06-22 14:39 心夢(mèng)帆影 閱讀(8892) 評(píng)論(2)  編輯  收藏 所屬分類: JavaSE

          評(píng)論:
          # re: 介紹一個(gè)好用的工具類庫(kù)commons-beanutils 2009-06-26 09:47 | keer2345
          具體是做什么用的  回復(fù)  更多評(píng)論
            
          # re: 介紹一個(gè)好用的工具類庫(kù)commons-beanutils 2009-06-30 07:34 | 過河卒

          @keer2345
          顧名思義就是對(duì)Bean (PoJo)的操作工具集.@keer2345  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 吉木乃县| 牟定县| 金堂县| 乐平市| 钟祥市| 富民县| 灌南县| 漳州市| 江阴市| 辽阳市| 阿巴嘎旗| 武乡县| 拜泉县| 广西| 邯郸县| 平远县| 海淀区| 新化县| 厦门市| 定日县| 南康市| 正宁县| 晋州市| 泗水县| 沙田区| 盘锦市| 泗阳县| 水城县| 新晃| 农安县| 天台县| 蓬安县| 邛崃市| 达孜县| 彭山县| 瓮安县| 舞阳县| 西平县| 长子县| 布尔津县| 宝鸡市|