当前位置: 首页 > 科技观察

为什么阿里巴巴禁止使用ApacheBeanutils进行属性拷贝?

时间:2023-03-14 09:55:32 科技观察

在日常开发中,我们经常需要给对象赋值,通常会调用它们的set/get方法。有时,如果我们要转换的两个对象的属性大致相同,我们会考虑使用属性复制工具。比如我们在代码中经常会把一个数据结构封装成DO、SDO、DTO、VO等,而这些bean中的属性大部分都是相同的,所以使用属性拷贝工具可以帮助我们节省大量的set和开始运作。市面上类似的工具有很多,比较常用的有1、SpringBeanUtils2、CglibBeanCopier3、ApacheBeanUtils4、ApachePropertyUtils5、Dozer那么,我们应该选择哪个工具类比较合适呢?为什么阿里巴巴Java开发手册中提到禁止使用ApacheBeanUtils?由于篇幅优先,这些工具的用法和区别,什么是浅拷贝和深拷贝不在本文讨论范围之内。本文主要侧重于比较这些库的性能。性能对比NoDataNoBB,让我们写代码来比较这些框架的性能。代码示例如下:首先定义一个PersonDO类:publicclassPersonDO{privateIntegerid;privateStringname;privateIntegerage;privateDatebirthday;//省略setter/getter}然后定义一个PersonDTO类:publicclassPersonDTO{privateStringname;privateIntegerage;privateDatebirthday;}然后编写测试类:使用SpringBeanUtils进行属性复制:privatevoidmappingBySpringBeanUtils(PersonDOpersonDO,inttimes){StopWatchstopwatch=newStopWatch();stopwatch.start();for(inti=0;icommons-beanutilscommons-beanutils1.9.4commons-loggingcommons-logging1.1.2org.springframeworkorg.springframework.beans3.1.1.RELEASEcglibcglib-nodep<版本>2.2.2net.sf.dozerdozer5.5.1org.slf4jslf4j-api1.7.7org.slf4jjul-to-slf4j1.7.7org.slf4jjcl-over-slf4j<版本>1.7.7org.slf4jlog4j-over-slf4j1.7.7org.slf4jslf4j-jdk141.7.7