欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決BeanUtils.copyProperties不支持復(fù)制集合的問題

 更新時間:2021年06月16日 09:20:38   作者:東京易冷  
這篇文章主要介紹了解決BeanUtils.copyProperties不支持復(fù)制集合的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

工作中,經(jīng)常使用Spring的工具類BeanUtils.copyProperties對bean屬性進(jìn)行復(fù)制,這里的復(fù)制屬于淺復(fù)制。且不能復(fù)制集合和數(shù)組。本文會對該工具進(jìn)行一些測試。

文末會提出復(fù)制集合屬性的解決方案

準(zhǔn)備工作:準(zhǔn)備測試需要的類

@Data
public class Class {
    private People[] member;
    private People teacher;
    private List<People> student;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class People {
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
}

測試代碼:測試BeanUtils.copyProperties是否支持復(fù)制數(shù)組和集合,還有解決方案

public static void main(String[] args) {
 // 測試數(shù)組的復(fù)制
 People[] member = new People[3];
 member[0] = new People(1, "老師", 30, 1);
 member[1] = new People(2, "班長", 15, 1);
 member[2] = new People(3, "學(xué)生", 15, 1);
 People[] member1 = new People[]{};
 BeanUtils.copyProperties(member, member1);
 System.out.println("是否可以復(fù)制數(shù)組:" + (member1.length == 0 ? false : true));
 // 測試List的復(fù)制(Map也不能復(fù)制,測試略)
 List<People> student = new ArrayList<>();
 student.add(member[1]);
 student.add(member[2]);
 List<People> student1 = new ArrayList<>();
 BeanUtils.copyProperties(student, student1);
 System.out.println("BeanUtils.copyProperties是否可以復(fù)制List:" + (student1.isEmpty() ? false : true));
 // 通過JSON工具實現(xiàn)List的復(fù)制(不僅僅是List,數(shù)組和Map等也可以通過類似方法實現(xiàn)復(fù)制,需要有無參構(gòu)造方法,否則報錯)
 student1 = JSON.parseArray(JSON.toJSONString(student), People.class);
 System.out.println("通過JSON工具復(fù)制List:" + student1);
 System.out.println("通過JSON工具是否深復(fù)制:" + (student.get(0) != student1.get(0) ? true : false));
 // 測試是否深復(fù)制
 Class source = new Class();
 source.setMember(member);
 source.setTeacher(member[0]);
 source.setStudent(student);
 Class target = new Class();
 BeanUtils.copyProperties(source, target);
 System.out.println("BeanUtils.copyProperties是否深復(fù)制:" + (source.getMember() != target.getMember() ? true : false));
}

測試結(jié)果

是否可以復(fù)制數(shù)組:false
BeanUtils.copyProperties是否可以復(fù)制List:false
通過JSON工具復(fù)制List:[People(id=2, name=班長, age=15, sex=1), People(id=3, name=學(xué)生, age=15, sex=1)]
通過JSON工具是否深復(fù)制:true
BeanUtils.copyProperties是否深復(fù)制:false

針對List的復(fù)制除了通過JSON工具,最簡單的就是循環(huán)復(fù)制集合屬性,下面測試兩種方法的效率。

public static void main(String[] args) {
 int count = 1;
 System.out.println("測試數(shù)據(jù)長度:" + count);
 List<People> source = new LinkedList<>();
 List<People> target = new LinkedList<>();
 long start;
 for (int i = 0; i < count; i++) {
     source.add(new People(1, "ly", 25, 1));
 }
 
 start = System.nanoTime();
 target = JSON.parseArray(JSON.toJSONString(source), People.class);
 System.out.println("JSON:" + (System.nanoTime() - start));
 
 start = System.nanoTime();
 for (int i = 0; i < count; i++) {
     People p = new People();
     BeanUtils.copyProperties(source.get(i), p);
     target.add(p);
 }
 System.out.println("BeanUtils.copyProperties" + (System.nanoTime() - start));
}

分別測試count=1、10、100、1000、10000、100000的結(jié)果。為了防止一起執(zhí)行出現(xiàn)影響,每次只測試一種復(fù)制方法的一種情況,共執(zhí)行12次。通過對比可以知道,通過JSON復(fù)制屬性快于BeanUtils,

測試數(shù)據(jù)長度:1
JSON:154767336
Bean:275182853
測試數(shù)據(jù)長度:10
JSON:165678435
Bean:275301421
測試數(shù)據(jù)長度:100
JSON:167937206
Bean:328461161
測試數(shù)據(jù)長度:1000
JSON:187832969
Bean:315815289
測試數(shù)據(jù)長度:10000
JSON:297461312
Bean:362763360
測試數(shù)據(jù)長度:100000
JSON:562035707
Bean:5815319343

通過以下方式解決復(fù)制List、Map

public static <T> List copyList(List<T> list) {
    if (CollectionUtils.isEmpty(list)) {
        return new ArrayList();
    }
    return JSON.parseArray(JSON.toJSONString(list), list.get(0).getClass());
}

public static Map<String, Object> copyMap(Map map) {
    return JSON.parseObject(JSON.toJSONString(map));
}

BeanUtils.copyProperties的用法和優(yōu)缺點

一、簡介

BeanUtils提供對Java反射和自省API的包裝。其主要目的是利用反射機(jī)制對JavaBean的屬性進(jìn)行處理。我們知道,一個JavaBean通常包含了大量的屬性,很多情況下,對JavaBean的處理導(dǎo)致大量get/set代碼堆積,增加了代碼長度和閱讀代碼的難度。

二、用法 

BeanUtils是這個包里比較常用的一個工具類,這里只介紹它的copyProperties()方法。該方法定義如下:

Java代碼

public static void copyProperties(java.lang.Object dest,java.lang.Object orig)   
throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException 

如果你有兩個具有很多相同屬性的JavaBean,一個很常見的情況就是Struts里的PO對象(持久對象)和對應(yīng)的ActionForm,例如 Teacher和TeacherForm。

我們一般會在Action里從ActionForm構(gòu)造一個PO對象,傳統(tǒng)的方式是使用類似下面的語句對屬性逐個賦值:

//得到TeacherForm   
TeacherForm teacherForm=(TeacherForm)form;   
  
//構(gòu)造Teacher對象   
Teacher teacher=new Teacher();   
  
//賦值   
teacher.setName(teacherForm.getName());   
teacher.setAge(teacherForm.getAge());   
teacher.setGender(teacherForm.getGender());   
teacher.setMajor(teacherForm.getMajor());   
teacher.setDepartment(teacherForm.getDepartment());   
  
//持久化Teacher對象到數(shù)據(jù)庫   
HibernateDAO.save(teacher);  

而使用BeanUtils后,代碼就大大改觀了,如下所示:

//得到TeacherForm   
TeacherForm teacherForm=(TeacherForm)form;   
  
//構(gòu)造Teacher對象   
Teacher teacher=new Teacher();   
  
//賦值   
BeanUtils.copyProperties(teacher,teacherForm);   
  
//持久化Teacher對象到數(shù)據(jù)庫   
HibernateDAO.save(teacher);  

如果Teacher和TeacherForm間存在名稱不相同的屬性,則BeanUtils不對這些屬性進(jìn)行處理,需要程序員手動處理。

例如 Teacher包含modifyDate(該屬性記錄最后修改日期,不需要用戶在界面中輸入)屬性而TeacherForm無此屬性,那么在上面代碼的 copyProperties()后還要加上一句:

teacher.setModifyDate(new Date());  

怎么樣,很方便吧!除BeanUtils外還有一個名為PropertyUtils的工具類,它也提供copyProperties()方法,作用與 BeanUtils的同名方法十分相似,主要的區(qū)別在于后者提供類型轉(zhuǎn)換功能,即發(fā)現(xiàn)兩個JavaBean的同名屬性為不同類型時,在支持的數(shù)據(jù)類型范圍內(nèi)進(jìn)行轉(zhuǎn)換,而前者不支持這個功能,但是速度會更快一些。

BeanUtils支持的轉(zhuǎn)換類型如下:

* java.lang.BigDecimal   
* java.lang.BigInteger   
* boolean and java.lang.Boolean   
* byte and java.lang.Byte     
* char and java.lang.Character    
* java.lang.Class   
* double and java.lang.Double    
* float and java.lang.Float
* int and java.lang.Integer   
* long and java.lang.Long   
* short and java.lang.Short   
* java.lang.String   
* java.sql.Date   
* java.sql.Time   
* java.sql.Timestamp  

這里要注意一點,java.util.Date是不被支持的,而它的子類java.sql.Date是被支持的。因此如果對象包含時間類型的屬性,且希望被轉(zhuǎn)換的時候,一定要使用java.sql.Date類型。否則在轉(zhuǎn)換時會提示argument mistype異常。

三、優(yōu)缺點 

Apache Jakarta Commons項目非常有用。我曾在許多不同的項目上或直接或間接地使用各種流行的commons組件。其中的一個強(qiáng)大的組件就是BeanUtils。我 將說明如何使用BeanUtils將local實體bean轉(zhuǎn)換為對應(yīng)的value 對象:

BeanUtils.copyProperties(aValue, aLocal)  

上面的代碼從aLocal對象復(fù)制屬性到aValue對象。它相當(dāng)簡單!它不管local(或?qū)?yīng)的value)對象有多少個屬性,只管進(jìn)行復(fù)制。我們假設(shè) local對象有100個屬性。

上面的代碼使我們可以無需鍵入至少100行的冗長、容易出錯和反復(fù)的get和set方法調(diào)用。這太棒了!太強(qiáng)大了!太有用 了!

現(xiàn)在,還有一個壞消息:使用BeanUtils的成本驚人地昂貴!我做了一個簡單的測試,BeanUtils所花費(fèi)的時間要超過取數(shù) 據(jù)、將其復(fù)制到對應(yīng)的 value對象(通過手動調(diào)用get和set方法),以及通過串行化將其返回到遠(yuǎn)程的客戶機(jī)的時間總和。所以要小心使用這種威力!

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論