为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?
点击上方 好好学java ,选择 星标 公众号
重磅资讯,干货,第一时间送达 今日推荐:分享一套基于SpringBoot和Vue的企业级中后台开源项目,这个项目有点哇塞!
个人原创100W +访问量博客:点击前往,查看更多
在日常开发中,我们经常需要给对象进行赋值,通常会调用其set/get方法,有些时候,如果我们要转换的两个对象之间属性大致相同,会考虑使用属性拷贝工具进行。
如我们经常在代码中会对一个数据结构封装成DO、SDO、DTO、VO等,而这些Bean中的大部分属性都是一样的,所以使用属性拷贝类工具可以帮助我们节省大量的set和get操作。
市面上有很多类似的工具类,比较常用的有
1、Spring BeanUtils
2、Cglib BeanCopier
3、Apache BeanUtils
4、Apache PropertyUtils
5、Dozer
那么,我们到底应该选择哪种工具类更加合适呢?为什么阿里巴巴Java开发手册中提到禁止使用Apache BeanUtils呢?
由于篇幅优先,关于这几种工具类的用法及区别,还有到底是什么是浅拷贝和深拷贝不在本文的讨论范围内。
本文主要聚焦于对比这几个类库的性能问题。
性能对比
No Data No BB,我们就来写代码来对比下这几种框架的性能情况。
代码示例如下:
首先定义一个PersonDO类:
public class PersonDO {
private Integer id;
private String name;
private Integer age;
private Date birthday;
//省略setter/getter
}
再定义一个PersonDTO类:
public class PersonDTO {
private String name;
private Integer age;
private Date birthday;
}
然后进行测试类的编写:
使用Spring BeanUtils进行属性拷贝:
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());
}
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());
}
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());
}
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());
}
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);
}
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>org.springframework.beansartifactId>
<version>3.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglib-nodepartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>net.sf.dozergroupId>
<artifactId>dozerartifactId>
<version>5.5.1version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jul-to-slf4jartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>jcl-over-slf4jartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>log4j-over-slf4jartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-jdk14artifactId>
<version>1.7.7version>
dependency>
推荐文章
原创电子书
历时整整一年总结的 Java面试+ Java入门技术学习指南,这是本人这几年及校招的总结,各种异步面试题已经全部进行总结,按照章节复习即可,已经拿到了了大厂提供。 原创思维导图
扫码或者微信搜 程序员的技术圈子 回复 面试 领取原创电子书和思维导图。