您好,登錄后才能下訂單哦!
這篇文章主要講解了“為什么禁止使用Apache Beanutils進(jìn)行屬性的copy”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“為什么禁止使用Apache Beanutils進(jìn)行屬性的copy”吧!
性能對(duì)比
No Data No BB,我們就來(lái)寫代碼來(lái)對(duì)比下這幾種框架的性能情況。
代碼示例如下:
首先定義一個(gè)PersonDO類:
public class PersonDO { private Integer id; private String name; private Integer age; private Date birthday; //省略setter/getter }
再定義一個(gè)PersonDTO類:
public class PersonDTO { private String name; private Integer age; private Date birthday; }
然后進(jìn)行測(cè)試類的編寫:
使用Spring BeanUtils進(jìn)行屬性拷貝:
private void mappingBySpringBeanUtils(PersonDO personDO, int times) { StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < times; i++) { PersonDTO personDTO = new PersonDTO(); org.springframework.beans.BeanUtils.copyProperties(personDO, personDTO); } stopwatch.stop(); System.out.println("mappingBySpringBeanUtils cost :" + stopwatch.getTotalTimeMillis()); }
其中的StopWatch用于記錄代碼執(zhí)行時(shí)間,方便進(jìn)行對(duì)比。
使用Cglib BeanCopier進(jìn)行屬性拷貝:
private void mappingByCglibBeanCopier(PersonDO personDO, int times) { StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < times; i++) { PersonDTO personDTO = new PersonDTO(); BeanCopier copier = BeanCopier.create(PersonDO.class, PersonDTO.class, false); copier.copy(personDO, personDTO, null); } stopwatch.stop(); System.out.println("mappingByCglibBeanCopier cost :" + stopwatch.getTotalTimeMillis()); }
使用Apache BeanUtils進(jìn)行屬性拷貝:
private void mappingByApacheBeanUtils(PersonDO personDO, int times) throws InvocationTargetException, IllegalAccessException { StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < times; i++) { PersonDTO personDTO = new PersonDTO(); BeanUtils.copyProperties(personDTO, personDO); } stopwatch.stop(); System.out.println("mappingByApacheBeanUtils cost :" + stopwatch.getTotalTimeMillis()); }
使用Apache PropertyUtils進(jìn)行屬性拷貝:
private void mappingByApachePropertyUtils(PersonDO personDO, int times) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { StopWatch stopwatch = new StopWatch(); stopwatch.start(); for (int i = 0; i < times; i++) { PersonDTO personDTO = new PersonDTO(); PropertyUtils.copyProperties(personDTO, personDO); } stopwatch.stop(); System.out.println("mappingByApachePropertyUtils cost :" + stopwatch.getTotalTimeMillis()); }
然后執(zhí)行以下代碼:
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { PersonDO personDO = new PersonDO(); personDO.setName("Hollis"); personDO.setAge(26); personDO.setBirthday(new Date()); personDO.setId(1); MapperTest mapperTest = new MapperTest(); mapperTest.mappingBySpringBeanUtils(personDO, 100); mapperTest.mappingBySpringBeanUtils(personDO, 1000); mapperTest.mappingBySpringBeanUtils(personDO, 10000); mapperTest.mappingBySpringBeanUtils(personDO, 100000); mapperTest.mappingBySpringBeanUtils(personDO, 1000000); mapperTest.mappingByCglibBeanCopier(personDO, 100); mapperTest.mappingByCglibBeanCopier(personDO, 1000); mapperTest.mappingByCglibBeanCopier(personDO, 10000); mapperTest.mappingByCglibBeanCopier(personDO, 100000); mapperTest.mappingByCglibBeanCopier(personDO, 1000000); mapperTest.mappingByApachePropertyUtils(personDO, 100); mapperTest.mappingByApachePropertyUtils(personDO, 1000); mapperTest.mappingByApachePropertyUtils(personDO, 10000); mapperTest.mappingByApachePropertyUtils(personDO, 100000); mapperTest.mappingByApachePropertyUtils(personDO, 1000000); mapperTest.mappingByApacheBeanUtils(personDO, 100); mapperTest.mappingByApacheBeanUtils(personDO, 1000); mapperTest.mappingByApacheBeanUtils(personDO, 10000); mapperTest.mappingByApacheBeanUtils(personDO, 100000); mapperTest.mappingByApacheBeanUtils(personDO, 1000000); }
得到結(jié)果如下:
工具類 | 執(zhí)行1000次耗時(shí) | 執(zhí)行10000次耗時(shí) | 執(zhí)行100000次耗時(shí) | 執(zhí)行1000000次耗時(shí) |
---|---|---|---|---|
Spring BeanUtils | 5ms | 10ms | 45ms | 169ms |
Cglib BeanCopier | 4ms | 18ms | 45ms | 91ms |
Apache PropertyUtils | 60ms | 265ms | 1444ms | 11492ms |
Apache BeanUtils | 138ms | 816ms | 4154ms | 36938ms |
Dozer | 566ms | 2254ms | 11136ms | 102965ms |
畫了一張折線圖更方便大家進(jìn)行對(duì)比
綜上,我們基本可以得出結(jié)論,在性能方面,Spring BeanUtils和Cglib BeanCopier表現(xiàn)比較不錯(cuò),而Apache PropertyUtils、Apache BeanUtils以及Dozer則表現(xiàn)的很不好。
所以,如果考慮性能情況的話,建議大家不要選擇Apache PropertyUtils、Apache BeanUtils以及Dozer等工具類。
很多人會(huì)不理解,為什么大名鼎鼎的Apache開源出來(lái)的的類庫(kù)性能確不高呢?這不像是Apache的風(fēng)格呀,這背后導(dǎo)致性能低下的原因又是什么呢?
其實(shí),是因?yàn)锳pache BeanUtils力求做得完美, 在代碼中增加了非常多的校驗(yàn)、兼容、日志打印等代碼,過度的包裝導(dǎo)致性能下降嚴(yán)重。
感謝各位的閱讀,以上就是“為什么禁止使用Apache Beanutils進(jìn)行屬性的copy”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)為什么禁止使用Apache Beanutils進(jìn)行屬性的copy這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。