2020-07-29 为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?

引出问题

在日常开发中,我们经常需要给对象进行赋值,通常会调用其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呢?

阿里巴巴手册

性能对比


实体类1


实体类2

测试代码:

package com.example.demo.bussniss.entity;

import org.apache.commons.beanutils.PropertyUtils;

import org.springframework.beans.BeanUtils;

import org.springframework.cglib.beans.BeanCopier;

import org.springframework.util.StopWatch;

import java.lang.reflect.InvocationTargetException;

import java.util.Date;

/**

* @author :suyanlong

* @date :Created in 2020/7/30 9:29

* @description:ceshi

* @version: 1.0

*/

public class PersonTest {

// 使用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("spring copy time:" + stopwatch.getTotalTimeMillis());

    }

// 使用Cglib BeanCopier进行属性拷贝:

    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("cglibs copy time:" + stopwatch.getTotalTimeMillis());

    }

// 使用Apache BeanUtils进行属性拷贝:

    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("apbean copy time:" + stopwatch.getTotalTimeMillis());

    }

// 使用Apache PropertyUtils进行属性拷贝:

    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("approp copy time:" + 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);

        PersonTest mapperTest =new PersonTest();

        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);

    }

}


测试结果

测试结果

综上,我们基本可以得出结论,在性能方面,Spring BeanUtils和Cglib BeanCopier表现比较不错,而Apache PropertyUtils、Apache BeanUtils以及Dozer则表现的很不好。

所以,如果考虑性能情况的话,建议大家不要选择Apache PropertyUtils、Apache BeanUtils以及Dozer等工具类。

很多人会不理解,为什么大名鼎鼎的Apache开源出来的的类库性能确不高呢?这不像是Apache的风格呀,这背后导致性能低下的原因又是什么呢?

其实,是因为Apache BeanUtils力求做得完美, 在代码中增加了非常多的校验、兼容、日志打印等代码,过度的包装导致性能下降严重。

总结

本文通过对比几种常见的属性拷贝的类库,分析得出了这些工具类的性能情况,最终也验证了《阿里巴巴Java开发手册》中提到的"Apache BeanUtils 效率低"的事实。

但是本文只是站在性能这一单一角度进行了对比,我们在选择一个工具类的时候还会有其他方面的考虑,比如使用成本、理解难度、兼容性、可扩展性等,对于这种拷贝类工具类,我们还会考虑其功能是否完善等。

就像虽然Dozer性能比较差,但是他可以很好的和Spring结合,可以通过配置文件等进行属性之间的映射等,也受到了很多开发者的喜爱。

深拷贝、浅拷贝参考:https://blog.csdn.net/weixin_39585051/article/details/107681667


参考:https://mp.weixin.qq.com/s/PxRnZ6rMVxMf4FksvjIvtA

你可能感兴趣的:(2020-07-29 为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?)