Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of several methods of Bean replication

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "introduction of several methods of Bean replication". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "introduction of several methods of Bean replication".

4 replication methods:

Apache BeanUtils

Apache PropertyUtils

Spring BeanUtils

Cglib BeanCopier

Test Bean,FromBean and ToBean properties are consistent

Public class FromBean {private String name; private int age; private double money; public double getMoney () {return money;} public void setMoney (double money) {this.money = money;} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age } public void setAge (int age) {this.age = age;}} Test method import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import org.springframework.cglib.beans.BeanCopier;public class Test {public static void main (String [] args) {FromBean fb = new FromBean (); fb.setAge (20) Fb.setMoney (30000.111); fb.setName (Test); int count = 10; convert ((tb)-> BeanUtils.copyProperties (tb, fb), count, "Apache BeanUtils.copyProperties"); convert ((tb)-> PropertyUtils.copyProperties (tb, fb), count, "Apache PropertyUtils.copyProperties") Convert ((tb)-> org.springframework.beans.BeanUtils.copyProperties (fb, tb), count, "Spring BeanUtils.copyProperties"); BeanCopier bc = BeanCopier.create (FromBean.class, ToBean.class, false); convert ((tb)-> bc.copy (fb, tb, null), count, "Cglib BeanCopier.copy") } public static void convert (ConvertBean cb, int count, String type) {try {System.out.println (type + "start testing"); long start = System.currentTimeMillis (); for (int I = 0; I < count) ToBean tobean +) {ToBean tobean = new ToBean (); cb.convertMethod (tobean); System.out.println (tobean.getAge ()); System.out.println (tobean.getMoney ()) System.out.println (tobean.getName ());} System.out.println ("time consuming" + (System.currentTimeMillis ()-start) + "ms"); System.out.println ("- -") } catch (Exception e) {System.out.println ("Exception:" + e.getMessage ()); e.printStackTrace ();} @ FunctionalInterfaceinterface ConvertBean {void convertMethod (ToBean tb) throws Exception } print the results Apache BeanUtils.copyProperties start testing takes 1178 ms--Apache PropertyUtils.copyProperties to start testing takes 5 ms--Spring BeanUtils.copyProperties to start testing takes 172 ms--Cglib BeanCopier.copy to start Line test takes 0 ms-- test result

Unit: ms

Count:10 number: 10, first, second, third, fourth average, single average, Apache BeanUtils1178102991710191035.75103.575Apache PropertyUtils54845.250.525Spring BeanUtils172257123135171.7517.175Cglib BeanCopier000000count:100, 100, first, second, third, fourth average, single average, Apache BeanUtils18681258162412601502.515.025Apache PropertyUtils1610181715.250.1525Spring BeanUtils193152209215192.251.9225Cglib BeanCopier000000count:10000, 10, 000, first, second, third, fourth average, Apache BeanUtils16111657178615721656.50.16565Apache PropertyUtils243641941111530.0153Spring BeanUtils5512174334314080.0408Cglib BeanCopier106376.50.00065 conclusion.

The larger the quantity, the less time it takes each time.

Comprehensive Cglib BeanCopier has the best performance, but it should be defined with BeanCopier.create before using it.

When Cglib tests, he caches the instance first, which is also the reason for his good performance.

Spring BeanUtils cannot convert java.util.Date and can support its subclass java.sql.Date

Thank you for your reading, the above is the content of "introduction of several methods of Bean replication". After the study of this article, I believe you have a deeper understanding of several methods of Bean replication, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report