日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Common BeanUtils 简介

發布時間:2024/4/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Common BeanUtils 简介 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Beanutils用了魔術般的反射技術,實現了很多夸張有用的功能,都是C/C++時代不敢想的。無論誰的項目,始終一天都會用得上它。我算是后知后覺了,第一回看到它的時候居然錯過。1.屬性的動態getter,setter 在這框架滿天飛的年代,不能事事都保證執行getter,setter函數了,有時候屬性是要需要根據名字動態取得的,就像這樣:   BeanUtils.getProperty(myBean,"code"); 而BeanUtils更強的功能是直接訪問內嵌對象的屬性,只要使用點號分隔。 BeanUtils.getProperty(orderBean, "address.city"); 相比之下其他類庫的BeanUtils通常都很簡單,不能訪問內嵌的對象,所以經常要用Commons BeanUtils替換它們。 BeanUtils還支持List和Map類型的屬性。如下面的語法即可取得顧客列表中第一個顧客的名字 BeanUtils.getProperty(orderBean, "customers[1].name"); 其中BeanUtils會使用ConvertUtils類把字符串轉為Bean屬性的真正類型,方便從HttpServletRequest等對象中提取bean,或者把bean輸出到頁面。 而PropertyUtils就會原色的保留Bean原來的類型。 2.beanCompartor 動態排序 還是通過反射,動態設定Bean按照哪個屬性來排序,而不再需要在bean的Compare接口進行復雜的條件判斷。 List peoples = ...; // Person對象的列表 Collections.sort(peoples, new BeanComparator("age"));如果要支持多個屬性的復合排序,如"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屬性不是普通類型,構造函數需要再傳入一個comparator對象為age變量排序。 另外, BeanCompartor本身的ComparebleComparator, 遇到屬性為null就會拋出異常, 也不能設定升序還是降序。 這個時候又要借助commons-collections包的ComparatorUtils.Comparator mycmp = ComparableComparator.getInstance();mycmp = ComparatorUtils.nullLowComparator(mycmp); //允許nullmycmp = ComparatorUtils.reversedComparator(mycmp); //逆序Comparator cmp = new BeanComparator(sortColumn, mycmp);3.Converter 把Request或ResultSet中的字符串綁定到對象的屬性 經常要從request,resultSet等對象取出值來賦入bean中,下面的代碼誰都寫膩了,如果不用MVC框架的綁定功能的話。String a = request.getParameter("a");bean.setA(a);String b = ....不妨寫一個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方法其實都會調用convert進行轉換。但Converter只支持一些基本的類型,甚至連java.util.Date類型也不支持。而且它比較笨的一個地方是當遇到不認識的類型時,居然會拋出異常來。對于Date類型,我參考它的sqldate類型實現了一個Converter,而且添加了一個設置日期格式的函數。 要把這個Converter注冊,需要如下語句:ConvertUtilsBean convertUtils = new ConvertUtilsBean(); DateConverter dateConverter = new DateConverter(); convertUtils.register(dateConverter,Date.class); //因為要注冊converter,所以不能再使用BeanUtils的靜態方法了,必須創建BeanUtilsBean實例 BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean()); beanUtils.setProperty(bean, name, value); 4 其他功能 4.1 PropertyUtils,當屬性為Collection,Map時的動態讀取:Collection: 提供indexBeanUtils.getIndexedProperty(orderBean,"items",1); 或者BeanUtils.getIndexedProperty(orderBean,"items[1]");Map: 提供Key ValueBeanUtils.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,動態創建對象public static Object invokeConstructor(Class klass, Object arg) 4.4 MethodUtils,動態調用方法MethodUtils.invokeMethod(bean, methodName, parameter); 4.5 動態Bean 見用DynaBean減除不必要的VO和FormBean

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的Common BeanUtils 简介的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。