當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils、Spring BeanUtils、Cglib BeanCopier)
生活随笔
收集整理的這篇文章主要介紹了
Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils、Spring BeanUtils、Cglib BeanCopier)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章轉載自:Bean復制的幾種框架性能比較
Bean復制的幾種框架性能比較:
測試結果:
| BeanUtil.copyProperties | 54 | 57 | 50 | 53.66667 | 5.366666667 |
| PropertyUtils.copyProperties | 4 | 4 | 4 | 4 | 0.4 |
| org.springframework.beans.BeanUtils.copyProperties | 12 | 10 | 11 | 11 | 1.1 |
| BeanCopier.create | 0 | 0 | 0 | 0 | 0 |
| BeanUtil.copyProperties | 241 | 222 | 226 | 229.6667 | 0.022966667 |
| PropertyUtils.copyProperties | 92 | 90 | 92 | 91.33333 | 0.009133333 |
| org.springframework.beans.BeanUtils.copyProperties | 29 | 30 | 32 | 30.33333 | 0.003033333 |
| BeanCopier.create | 1 | 1 | 1 | 1 | 0.1 |
| BeanUtil.copyProperties | 178 | 174 | 178 | 176.6667 | 0.017666667 |
| PropertyUtils.copyProperties | 91 | 87 | 89 | 89 | 0.0089 |
| org.springframework.beans.BeanUtils.copyProperties | 21 | 21 | 21 | 21 | 0.0021 |
| BeanCopier.create | 0 | 1 | 1 | 0.666667 | 6.66667E-05 |
注意:Cglib在測試的時候,先進行了實例的緩存,這個也是他性能較好的原因之一。如果把緩存去掉的話,性能就會出現了一些的差異,但是整體的性能還是很好,不過奇怪的是10000次反而比10次少,而且后面的反轉1萬次反而耗時最少,進行多次測試效果也是如此。
從整體的表現來看:
- Cglib的BeanCopier的性能是最好的無論是數量較大的1萬次的測試,還是數量較少10次,幾乎都是趨近與零損耗。
- Spring是在次數增多的情況下,性能較好,在數據較少的時候,性能比PropertyUtils的性能差一些。
- PropertyUtils的性能相對穩定,表現是呈現線性增長的趨勢。
- 而Apache的BeanUtil的性能最差,無論是單次Copy還是大數量的多次Copy性能都不是很好
| BeanCopier.create | 41 | 28 | 10 |
cglib BeanCopier 使用小探究:
- cglib是一款比較底層的操作java字節碼的框架,其特性包括:
注意:即使源類型是原始類型(int, short和char等),目標類型是其包裝類型(Integer, Short和Character等),或反之:都不會被拷貝。
網上很多文章說是這里拋得異常:
我信你個鬼,糟老頭子
Debug追蹤看一下代碼執行過程,根本就沒有執行generateClass方法中的代碼呢(如果寫錯了,歡迎大家來吐槽,一起分析),也沒有拋空指針異常,難道版本不一樣嗎!
當源和目標類的屬性類型不同時,不能拷貝該屬性,此時我們可以通過實現Converter接口來自定義轉換器:
直接上代碼吧:
package com.pica.cloud.patient.resident.server;import java.sql.Timestamp;/*** @ClassName SourceClazz* @Description 源類* @Author Chongwen.jiang* @Date 2019/7/25 17:17* @ModifyDate 2019/7/25 17:17* @Version 1.0*/ public class SourceClazz {private Integer id;private String name;private Timestamp createTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Timestamp getCreateTime() {return createTime;}public void setCreateTime(Timestamp createTime) {this.createTime = createTime;} }
package com.pica.cloud.patient.resident.server;import org.springframework.cglib.core.Converter;import java.math.BigDecimal; import java.sql.Timestamp; import java.text.SimpleDateFormat;/*** @ClassName AccountConverter* @Description Bean屬性類型轉換器* @Author Chongwen.jiang* @Date 2019/7/25 17:56* @ModifyDate 2019/7/25 17:56* @Version 1.0*/ public class TypeConverter implements Converter {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Overridepublic Object convert(Object value, Class target, Object o1) {if (value instanceof Integer) {return (Integer) value;} else if (value instanceof Timestamp) {Timestamp date = (Timestamp) value;return sdf.format(date);} else if (value instanceof BigDecimal) {BigDecimal bd = (BigDecimal) value;return bd.toPlainString();}return null;} }
package com.pica.cloud.patient.resident.server;import com.alibaba.fastjson.JSON; import com.pica.cloud.patient.resident.server.mapper.WechatUserMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cglib.beans.BeanCopier; import org.springframework.test.context.junit4.SpringRunner;import java.sql.Timestamp;/*** @ClassName DbTest* @Description 測試類* @Author Jun.li0101* @Date 2019/3/21 13:25* @ModifyDate 2019/3/21 13:25* @Version 1.0*/ @SpringBootTest @RunWith(SpringRunner.class) public class DbTest {@Autowiredprivate WechatUserMapper wechatUserMapper;@Testpublic void test1() {SourceClazz sourceClazz = new SourceClazz();sourceClazz.setId(12);sourceClazz.setName("loname");sourceClazz.setCreateTime(Timestamp.valueOf("2019-07-25 15:54:23"));final BeanCopier a = BeanCopier.create(SourceClazz.class, TargetClazz.class, true);TargetClazz targetClazz = new TargetClazz();TypeConverter typeConvert = new TypeConverter();a.copy(sourceClazz, targetClazz, typeConvert);System.out.println(JSON.toJSONString(targetClazz));Assert.assertEquals("2019-07-25 15:54:23", targetClazz.getCreateTime());} }
注:一旦使用Converter,BeanCopier只使用Converter定義的規則去拷貝屬性,所以在convert方法中要考慮所有的屬性。
總結
以上是生活随笔為你收集整理的Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils、Spring BeanUtils、Cglib BeanCopier)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS 基本样式大全(一)
- 下一篇: Java中的随机数