温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

为什么禁止使用Apache Beanutils进行属性的copy

发布时间:2021-10-28 15:43:36 来源:亿速云 阅读:151 作者:iii 栏目:编程语言

这篇文章主要讲解了“为什么禁止使用Apache Beanutils进行属性的copy”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“为什么禁止使用Apache Beanutils进行属性的copy”吧!

性能对比

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

其中的StopWatch用于记录代码执行时间,方便进行对比。

使用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("mappingByCglibBeanCopier cost :" + 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("mappingByApacheBeanUtils cost :" + 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("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); }

得到结果如下:

工具类执行1000次耗时执行10000次耗时执行100000次耗时执行1000000次耗时
Spring BeanUtils5ms10ms45ms169ms
Cglib BeanCopier4ms18ms45ms91ms
Apache PropertyUtils60ms265ms1444ms11492ms
Apache BeanUtils138ms816ms4154ms36938ms
Dozer566ms2254ms11136ms102965ms

画了一张折线图更方便大家进行对比

为什么禁止使用Apache Beanutils进行属性的copy

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

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

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

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

感谢各位的阅读,以上就是“为什么禁止使用Apache Beanutils进行属性的copy”的内容了,经过本文的学习后,相信大家对为什么禁止使用Apache Beanutils进行属性的copy这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI