In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "why it is forbidden to use Apache Beanutils for attribute copy". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Why is it forbidden to use Apache Beanutils for attribute copy"?
Performance comparison
No Data No BB, let's write code to compare the performance of these frameworks.
The code example is as follows:
First define a PersonDO class:
Public class PersonDO {private Integer id; private String name; private Integer age; private Date birthday; / / omit setter/getter}
Define another PersonDTO class:
Public class PersonDTO {private String name; private Integer age; private Date birthday;}
Then write the test class:
Use Spring BeanUtils to copy attributes:
Private void mappingBySpringBeanUtils (PersonDO personDO, int times) {StopWatch stopwatch = new StopWatch (); stopwatch.start (); for (int I = 0; I < times; ionization +) {PersonDTO personDTO = new PersonDTO (); org.springframework.beans.BeanUtils.copyProperties (personDO, personDTO);} stopwatch.stop (); System.out.println ("mappingBySpringBeanUtils cost:" + stopwatch.getTotalTimeMillis ());}
The StopWatch is used to record the execution time of the code for easy comparison.
Use Cglib BeanCopier to copy attributes:
Private void mappingByCglibBeanCopier (PersonDO personDO, int times) {StopWatch stopwatch = new StopWatch (); stopwatch.start (); for (int I = 0; I < times; iTunes +) {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 ();}
Use Apache BeanUtils to copy attributes:
Private void mappingByApacheBeanUtils (PersonDO personDO, int times) throws InvocationTargetException, IllegalAccessException {StopWatch stopwatch = new StopWatch (); stopwatch.start (); for (int I = 0; I < times; iTunes +) {PersonDTO personDTO = new PersonDTO (); BeanUtils.copyProperties (personDTO, personDO);} stopwatch.stop (); System.out.println ("mappingByApacheBeanUtils cost:" + stopwatch.getTotalTimeMillis ());}
Use Apache PropertyUtils to copy attributes:
Private void mappingByApachePropertyUtils (PersonDO personDO, int times) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {StopWatch stopwatch = new StopWatch (); stopwatch.start (); for (int I = 0; I < times; iTunes +) {PersonDTO personDTO = new PersonDTO (); PropertyUtils.copyProperties (personDTO, personDO);} stopwatch.stop (); System.out.println ("mappingByApachePropertyUtils cost:" + stopwatch.getTotalTimeMillis ());}
Then execute the following code:
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, 1000); mapperTest.mappingBySpringBeanUtils (personDO, 1000); mapperTest.mappingBySpringBeanUtils (personDO, 10000) MapperTest.mappingBySpringBeanUtils (personDO, 100000); mapperTest.mappingBySpringBeanUtils (personDO, 1000000); mapperTest.mappingByCglibBeanCopier (personDO, 1000); mapperTest.mappingByCglibBeanCopier (personDO, 1000); mapperTest.mappingByCglibBeanCopier (personDO, 10000); mapperTest.mappingByCglibBeanCopier (personDO, 100000); mapperTest.mappingByCglibBeanCopier (personDO, 1000000); mapperTest.mappingByApachePropertyUtils (personDO, 1000000); mapperTest.mappingByApachePropertyUtils (personDO, 1000000); mapperTest.mappingByApachePropertyUtils (personDO, 1000000); mapperTest.mappingByApachePropertyUtils (personDO, 100000) MapperTest.mappingByApachePropertyUtils (personDO, 1000000); mapperTest.mappingByApacheBeanUtils (personDO, 1000); mapperTest.mappingByApacheBeanUtils (personDO, 1000); mapperTest.mappingByApacheBeanUtils (personDO, 10000); mapperTest.mappingByApacheBeanUtils (personDO, 100000); mapperTest.mappingByApacheBeanUtils (personDO, 1000000);}
The results are as follows:
Tool class 1000 time-consuming execution 10000 time-consuming execution 100000 time-consuming execution 1000000 time-consuming Spring BeanUtils5ms10ms45ms169msCglib BeanCopier4ms18ms45ms91msApache PropertyUtils60ms265ms1444ms11492msApache BeanUtils138ms816ms4154ms36938msDozer566ms2254ms11136ms102965ms
Draw a line chart to make it easier for you to compare.
To sum up, we can basically conclude that in terms of performance, Spring BeanUtils and Cglib BeanCopier perform well, while Apache PropertyUtils, Apache BeanUtils and Dozer perform poorly.
Therefore, if you consider the performance situation, it is recommended that you do not choose tool classes such as Apache PropertyUtils, Apache BeanUtils, and Dozer.
Many people will not understand why the performance of the open source library from the famous Apache is really not high. This is not like the style of Apache, but what is the reason behind this poor performance?
In fact, it is because Apache BeanUtils strives to do it perfectly, adding a lot of verification, compatibility, log printing and other code to the code, and excessive packaging leads to serious performance degradation.
Thank you for your reading, the above is the content of "Why is it forbidden to use Apache Beanutils for attribute copy", after the study of this article, I believe you have a deeper understanding of why it is forbidden to use Apache Beanutils for attribute copy, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.