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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

BeanUtils,PropertyUtils

發布時間:2024/8/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BeanUtils,PropertyUtils 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:http://www.cnblogs.com/friends-wf/p/3720348.html

beanUtils的用法

舉例1:使用BeanUtils工具封裝用戶提交的數據。

1 public static void main(String[] args)throws Exception { 2 3 // 模擬用戶的輸入的數據如下 4 5 String name = "XML基礎"; 6 7 String author = "焦寧波"; 8 9 String price = "99.99"; 10 11 String date = "2013-01-04"; 12 13 Book book = new Book(); 14 15 // 任務是將以上的屬性設置給指定的Book對象 16 17 BeanUtils.setProperty(book, "name", name); 18 19 BeanUtils.setProperty(book, "author", author); 20 21 BeanUtils.setProperty(book, "price",price ); 22 23 // 查看屬性是否封裝好 24 25 System.out.println(book); 26 27 }

?

發現使用上面的代碼可以省略基本數據類型的轉型的問題。進而提高代碼的開發效率。

舉例2:自定義一個類型轉換器類。

1 public static void main(String[] args)throws Exception { 2 3 // 模擬用戶的輸入的數據如下 4 5 String name = "XML基礎"; 6 7 String author = "焦寧波"; 8 9 String price = "99.99"; 10 11 String date = "2013-01-04"; 12 13 14 15 Book book = new Book(); 16 17 18 19 // 注冊一個自己的轉換器 20 21 /** 22 23 * converter指定具體的轉換器 24 25 * clazz遇到什么類型調用上面的轉換器 26 27 */ 28 29 ConvertUtils.register( 30 31 new Converter(){ 32 33 // 回調方法 34 35 @Override 36 37 public Object convert(Class type, Object value) { 38 39 if(value == null){ 40 41 return null; 42 43 } 44 45 // 轉換為String 46 47 String data = (String)value; 48 49 // 將指定格式的字符串轉換為Date 50 51 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 52 53 Date date = null; 54 55 try { 56 57 date = format.parse(data); 58 59 return date; 60 61 } catch (ParseException e) { 62 63 e.printStackTrace(); 64 65 return null; 66 67 } 68 69 } 70 71 }, 72 73 Date.class); 74 75 // 任務是將以上的屬性設置給指定的Book對象 76 77 BeanUtils.setProperty(book, "name", name); 78 79 BeanUtils.setProperty(book, "author", author); 80 81 BeanUtils.setProperty(book, "price",price ); 82 83 BeanUtils.setProperty(book, "date",date ); 84 85 // 查看屬性是否封裝好 86 87 System.out.println(book); 88 89 }

?

如果每次遇到一個復雜類型都需要自定義轉換器,那樣的話實在麻煩。大家看在開發的時候可以先查看該接口是否提供了有效的實現類。

ConvertUtils.register(new DateLocaleConverter(), Date.class);

其實真正的封裝好的數據需要存儲在數據庫中,那么javabean的數據類型應該和數據庫的數據類型保持一致,那么在聲明持久化javabean的時候需要全部為數據庫的基本數據類型。

因此大家在JavaBean中需要導入的是java.sql.Date類,這樣就直接可以將日期自動轉換了。

舉例3:實現封裝好的JavaBean對象的屬性拷貝。

1 // 實現屬性封裝數據的一個拷貝 2 3 Book copy = new Book(); 4 5 System.out.println(copy); 6 7 PropertyUtils.copyProperties(copy, book); 8 9 System.out.println(copy);

?

思考:如果使用BeanUtils封裝用戶的數據,那么也就是一個一個設置啊?豈不是也很麻煩?

其實在真是的環境中我們可以直接獲取用戶提交的所有的數據信息,只需要進行遍歷即可,但是為了方便快速的設置,那么可以將javabean中的屬性名和用戶提交的數據名保持一致。


1、 BeanUtils一共分4個包:

?

?org.apache.commons.beanutils

?org.apache.commons.beanutils.converters

?org.apache.commons.beanutils.locale

?org.apache.commons.beanutils.locale.converters

?

其中上面兩個是BeanUtils的默認實現,它沒有針對本地化的任何處理,這個可以提高執行效率。

但是若你的程序對于本地化有要求的話,那還是使用下面2個包比較安全。

?

?org.apache.commons.beanutils

?

?這個包主要提供用于操作JavaBean的工具類,Jakarta-Common-BeanUtils的主要功能都在這個包里實現。

?

?

?

2、BeanUtils可以直接get和set一個屬性的值。它將property分成3種類型:???

?

?? ? ? Simple——簡單類型,如Stirng、Int…… ??

?

?? ? ? Indexed——索引類型,如數組、arrayList…… ??

?

?? ? ? Maped——這個不用說也該知道,就是指Map啦,比如HashMap…… ??

?

?? ? ? 訪問不同類型的數據可以直接調用函數getProperty和setProperty。getProperty只有2個參數,第一個是JavaBean對象,第二個是要操作的屬性名;setProperty提供三個參數,第一是JavaBean對象,第二個是要操作的屬性名,第三個為要設置的具體的值


public class Company { private String name; private HashMap address = new HashMap(); private String[] otherInfo; private ArrayList product; private ArrayList employee; private HashMap telephone; public Company(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress(String type) { return address.get(type).toString(); } public void setAddress(String type, String address) { this.address.put(type,address); } public String[] getOtherInfo() { return otherInfo; } public void setOtherInfo(String[] otherInfo) { this.otherInfo = otherInfo; } public ArrayList getProduct() { return product; } public void setProduct(ArrayList product) { this.product = product; } public ArrayList getEmployee() { return employee; } public void setEmployee(ArrayList employee) { this.employee = employee; } public HashMap getTelephone() { return telephone; } public void setTelephone(HashMap telephone) { this.telephone = telephone; } } ?

?

(1)Simple

//對于Simple類型,參數二直接是屬性名即可

?System.out.println(BeanUtils.getProperty(c, "name"));

?

(2)Map

//對于Map類型,則需要以“屬性名(key值)”的形式

?System.out.println(BeanUtils.getProperty(c, "address (A2)"));

?HashMap am = new HashMap();

am.put("1","234-222-1222211");

am.put("2","021-086-1232323");

?BeanUtils.setProperty(c,"telephone",am);

System.out.println(BeanUtils.getProperty(c, "telephone (2)"));

?

Map類型也可以采用BeanUtils.getMappedProperty(c,?"address","A2");

?

(3)index?

//對于Indexed,則為“屬性名[索引值]”,注意這里對于ArrayList和數組都可以用一樣的方式進行操作。

System.out.println(BeanUtils.getProperty(c, "otherInfo[2]"));

BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");

System.out.println(BeanUtils.getProperty(c, "product[1]"));

?

index類型還可以采用BeanUtils.getIndexedProperty(c,"product",1);

?

?(4)nest 3種類也可以組合使用啦!

System.out.println(BeanUtils.getProperty(c, "employee[1].name"));

?

3、bean copy功能?

?

?? ? ? Company c2 = new Company();

?? ? ? BeanUtils.copyProperties(c2, c);

?

?? ? ? 但是這種copy都是淺拷貝,復制后的2個Bean的同一個屬性可能擁有同一個對象的ref,這個在使用時要小心,特別是對于屬性為自定義類的情況

?

?

4、?BeanUtils和PropertyUtils

?????? 這兩個類幾乎有一摸一樣的功能,唯一的區別是:BeanUtils在對Bean賦值是會進行類型轉化。舉例來說也就是在copyProperty時只要屬性名相同,就算類型不同,BeanUtils也可以進行copy;而PropertyBean則可能會報錯!!

?????? 針對上面的例子,新建一個Company2的類,其中代碼與Company一樣,只是將otherinfo從String[]改為String。

????? Company c = init();

????? Company2 c2 = new Company2();

?????

????? BeanUtils.copyProperties(c2,c);

//??? PropertyUtils.copyProperties(c2,c); 這句會報錯!!

????? System.out.println(c2.getOtherInfo());

??? 當然2個Bean之間的同名屬性的類型必須是可以轉化的,否則用BeanUtils一樣會報錯。

?????? 若實現了org.apache.commons.beanutils.Converter接口則可以自定義類型之間的轉化。

由于不做類型轉化,用PropertyUtils在速度上會有很大提高!

此外,不作類型轉化還有一個好處,如下面的代碼:

????? //test data type convert

//??? ArrayList a1 = BeanUtils.getProperty(c,"product"); //BeanUtils返回的是String

????? System.out.println("--" + BeanUtils.getProperty(c,"product"));???? //取出后直接被轉為String

????? ArrayList a = (ArrayList)PropertyUtils.getProperty(c,"product");//PropertyUtils返回的是Object

????? System.out.println("--" + a.get(1));

用BeanUtils無法返回一個對象(除非自己寫一個Converter),它會自動進行類型轉化,然后返回String。對于想返回java類或自定義類的話,還是請使用PropertyUtils

?

5、Utils類

?????? 所有的XXXUtils類都提供的是靜態方法,可以直接調用,其主要實現都在相應的XXXUtilsBean中:

BeanUtils???? ——> BeanUtilsBean

?????? ConvertUtils? ——> ConvertUtilsBean

??? PropertyUtils ——> PropertyUtilsBean

?(1)PropertyUtils,獲取屬性的Class類型

public?static?Class?getPropertyType(Object?bean,?String?name)

(2)ConstructorUtils,動態創建對象

?public?static?Object?invokeConstructor(Class?klass,?Object?arg)

(3)MethodUtils,動態調用方法

?MethodUtils.invokeMethod(bean,?methodName,?parameter);


總結

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

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