這篇文章主要講解了“Bean復(fù)制的幾種方法介紹”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Bean復(fù)制的幾種方法介紹”吧!
成都創(chuàng)新互聯(lián)公司長期為超過千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鼓樓企業(yè)提供專業(yè)的網(wǎng)站制作、做網(wǎng)站,鼓樓網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
Apache BeanUtils
Apache PropertyUtils
Spring BeanUtils
Cglib BeanCopier
測試 Bean,F(xiàn)romBean 與 ToBean 屬性一致
public class FromBean { private String name; private int age; private double money; public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; import org.springframework.cglib.beans.BeanCopier; public class Test { public static void main(String[] args) { FromBean fb = new FromBean(); fb.setAge(20); fb.setMoney(30000.111); fb.setName("測試"); int count = 10; convert((tb) -> BeanUtils.copyProperties(tb, fb), count, "Apache BeanUtils.copyProperties"); convert((tb) -> PropertyUtils.copyProperties(tb, fb), count, "Apache PropertyUtils.copyProperties"); convert((tb) -> org.springframework.beans.BeanUtils.copyProperties(fb, tb), count, "Spring BeanUtils.copyProperties"); BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class, false); convert((tb) -> bc.copy(fb, tb, null), count, "Cglib BeanCopier.copy"); } public static void convert(ConvertBean cb, int count, String type) { try { System.out.println(type + "開始進(jìn)行測試"); long start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { ToBean tobean = new ToBean(); cb.convertMethod(tobean); System.out.println(tobean.getAge()); System.out.println(tobean.getMoney()); System.out.println(tobean.getName()); } System.out.println("耗時 " + (System.currentTimeMillis() - start) + " ms"); System.out.println("----------------------"); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } } } @FunctionalInterface interface ConvertBean { void convertMethod(ToBean tb) throws Exception; }
Apache BeanUtils.copyProperties開始進(jìn)行測試 耗時 1178 ms ---------------------- Apache PropertyUtils.copyProperties開始進(jìn)行測試 耗時 5 ms ---------------------- Spring BeanUtils.copyProperties開始進(jìn)行測試 耗時 172 ms ---------------------- Cglib BeanCopier.copy開始進(jìn)行測試 耗時 0 ms ----------------------
單位:ms
次數(shù):10 | 第一次 | 第二次 | 第三次 | 第四次 | 平均值 | 單次平均值 |
---|---|---|---|---|---|---|
Apache BeanUtils | 1178 | 1029 | 917 | 1019 | 1035.75 | 103.575 |
Apache PropertyUtils | 5 | 4 | 8 | 4 | 5.25 | 0.525 |
Spring BeanUtils | 172 | 257 | 123 | 135 | 171.75 | 17.175 |
Cglib BeanCopier | 0 | 0 | 0 | 0 | 0 | 0 |
次數(shù):100 | 第一次 | 第二次 | 第三次 | 第四次 | 平均值 | 單次平均值 |
---|---|---|---|---|---|---|
Apache BeanUtils | 1868 | 1258 | 1624 | 1260 | 1502.5 | 15.025 |
Apache PropertyUtils | 16 | 10 | 18 | 17 | 15.25 | 0.1525 |
Spring BeanUtils | 193 | 152 | 209 | 215 | 192.25 | 1.9225 |
Cglib BeanCopier | 0 | 0 | 0 | 0 | 0 | 0 |
次數(shù):10000 | 第一次 | 第二次 | 第三次 | 第四次 | 平均值 | 單次平均值 |
---|---|---|---|---|---|---|
Apache BeanUtils | 1611 | 1657 | 1786 | 1572 | 1656.5 | 0.16565 |
Apache PropertyUtils | 243 | 64 | 194 | 111 | 153 | 0.0153 |
Spring BeanUtils | 551 | 217 | 433 | 431 | 408 | 0.0408 |
Cglib BeanCopier | 10 | 6 | 3 | 7 | 6.5 | 0.00065 |
數(shù)量越大平均每次耗時越少
綜合 Cglib BeanCopier 性能最好,但在使用前,要先用 BeanCopier.create 定義
Cglib在測試的時候,先進(jìn)行了實例的緩存,這個也是他性能較好的原因
Spring BeanUtils 不能轉(zhuǎn)化 java.util.Date,可以支持它的子類 java.sql.Date
感謝各位的閱讀,以上就是“Bean復(fù)制的幾種方法介紹”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Bean復(fù)制的幾種方法介紹這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
分享題目:Bean復(fù)制的幾種方法介紹
URL分享:http://chinadenli.net/article24/ihpdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器、商城網(wǎng)站、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)