athrunwang

          紀(jì)元
          數(shù)據(jù)加載中……
          Beanutils基本用法

          Beanutils用了魔術(shù)般的反射技術(shù),實(shí)現(xiàn)了很多夸張有用的功能,都是C/C++時(shí)代不敢想的。無論誰的項(xiàng)目,始終一天都會(huì)用得上它。我算是后知后覺了,第一回看到它的時(shí)候居然錯(cuò)過。

          1.屬性的動(dòng)態(tài)getter,setter

          在這框架滿天飛的年代,不能事事都保證執(zhí)行g(shù)etter,setter函數(shù)了,有時(shí)候?qū)傩允且枰鶕?jù)名字動(dòng)態(tài)取得的,就像這樣:  
          BeanUtils.getProperty(myBean,"code");
          而BeanUtils更強(qiáng)的功能是直接訪問內(nèi)嵌對(duì)象的屬性,只要使用點(diǎn)號(hào)分隔。
          BeanUtils.getProperty(orderBean, "address.city");
          相比之下其他類庫(kù)的BeanUtils通常都很簡(jiǎn)單,不能訪問內(nèi)嵌的對(duì)象,所以經(jīng)常要用Commons BeanUtils替換它們。
          BeanUtils還支持List和Map類型的屬性。如下面的語法即可取得顧客列表中第一個(gè)顧客的名字
          BeanUtils.getProperty(orderBean, "customers[1].name");
          其中BeanUtils會(huì)使用ConvertUtils類把字符串轉(zhuǎn)為Bean屬性的真正類型,方便從HttpServletRequest等對(duì)象中提取bean,或者把bean輸出到頁面。
          而PropertyUtils就會(huì)原色的保留Bean原來的類型。

          2.beanCompartor 動(dòng)態(tài)排序

          還是通過反射,動(dòng)態(tài)設(shè)定Bean按照哪個(gè)屬性來排序,而不再需要在bean的Compare接口進(jìn)行復(fù)雜的條件判斷。
          List peoples = ...; // Person對(duì)象的列表Collections.sort(peoples, new BeanComparator("age"));

          如果要支持多個(gè)屬性的復(fù)合排序,如"Order By lastName,firstName"

          ArrayList sortFields = new ArrayList();sortFields.add(new BeanComparator("lastName"));
          sortFields.add(new BeanComparator("firstName"));
          ComparatorChain multiSort = new ComparatorChain(sortFields);
          Collections.sort(rows,multiSort);

          其中ComparatorChain屬于jakata commons-collections包。
          如果age屬性不是普通類型,構(gòu)造函數(shù)需要再傳入一個(gè)comparator對(duì)象為age變量排序。
          另外, BeanCompartor本身的ComparebleComparator, 遇到屬性為null就會(huì)拋出異常, 也不能設(shè)定升序還是降序。
          這個(gè)時(shí)候又要借助commons-collections包的ComparatorUtils.

             Comparator mycmp = ComparableComparator.getInstance();
             mycmp = ComparatorUtils.nullLowComparator(mycmp);  //允許null
             mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序
             Comparator cmp = new BeanComparator(sortColumn, mycmp);

          3.Converter 把Request或ResultSet中的字符串綁定到對(duì)象的屬性

             經(jīng)常要從request,resultSet等對(duì)象取出值來賦入bean中,下面的代碼誰都寫膩了,如果不用MVC框架的綁定功能的話。

             String a = request.getParameter("a");   bean.setA(a);   String b = ....

          不妨寫一個(gè)Binder:

               MyBean bean = ...;    HashMap map = new HashMap();    Enumeration names = request.getParameterNames();    while (names.hasMoreElements())    {      String name = (String) names.nextElement();      map.put(name, request.getParameterValues(name));    }    BeanUtils.populate(bean, map);

              其中BeanUtils的populate方法或者getProperty,setProperty方法其實(shí)都會(huì)調(diào)用convert進(jìn)行轉(zhuǎn)換。
              但Converter只支持一些基本的類型,甚至連java.util.Date類型也不支持。而且它比較笨的一個(gè)地方是當(dāng)遇到不認(rèn)識(shí)的類型時(shí),居然會(huì)拋出異常來。
              對(duì)于Date類型,我參考它的sqldate類型實(shí)現(xiàn)了一個(gè)Converter,而且添加了一個(gè)設(shè)置日期格式的函數(shù)。
          要把這個(gè)Converter注冊(cè),需要如下語句:

              ConvertUtilsBean convertUtils = new ConvertUtilsBean();
             DateConverter dateConverter = new DateConverter();
             convertUtils.register(dateConverter,Date.class);



          //因?yàn)橐?cè)converter,所以不能再使用BeanUtils的靜態(tài)方法了,必須創(chuàng)建BeanUtilsBean實(shí)例
          BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());
          beanUtils.setProperty(bean, name, value);
          4 其他功能
          4.1 PropertyUtils,當(dāng)屬性為Collection,Map時(shí)的動(dòng)態(tài)讀取:
           
          Collection: 提供index
             BeanUtils.getIndexedProperty(orderBean,"items",1);
          或者
            BeanUtils.getIndexedProperty(orderBean,"items[1]");

          Map: 提供Key Value
            BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111 
          或者
            BeanUtils.getMappedProperty(orderBean, "items(111)")
           
          4.2 PropertyUtils,獲取屬性的Class類型
               public static Class getPropertyType(Object bean, String name)
           
          4.3 ConstructorUtils,動(dòng)態(tài)創(chuàng)建對(duì)象
                public static Object invokeConstructor(Class klass, Object arg)
          4.4 MethodUtils,動(dòng)態(tài)調(diào)用方法
              MethodUtils.invokeMethod(bean, methodName, parameter);
          4.5 動(dòng)態(tài)Bean 用DynaBean減除不必要的VO和FormBean 

          posted on 2012-06-06 10:11 AthrunWang 閱讀(326) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 温泉县| 昌都县| 吉木乃县| 嘉禾县| 金溪县| 金乡县| 广饶县| 长岛县| 顺平县| 望江县| 嘉义县| 双桥区| 中阳县| 靖州| 南京市| 温州市| 买车| 南平市| 武安市| 赣州市| 鲁甸县| 海城市| 上栗县| 津市市| 陆川县| 米林县| 五寨县| 兰西县| 来安县| 临江市| 文安县| 甘德县| 隆子县| 石门县| 阳信县| 灵丘县| 陆丰市| 义乌市| 简阳市| 肥东县| 沙湾县|