Java Bean Attribute Copy tool analogy pair (performance + function)

Source: Internet
Author: User
Tags tojson

It is often necessary to copy the properties of two objects in a business system, and it is not possible to deny that the individual object copies are the fastest and safest way, but the code becomes bloated when the number of attribute fields of the data object exceeds the level of tolerance of the programmer, such as usually more than 5 attributes. Using some handy object Copy tool classes will be a good choice.

The more commonly accepted tool classes are now popular:

Two versions of Apache: (Reflection mechanism)

Org.apache.commons.beanutils.PropertyUtils.copyProperties (Object dest, Object orig)

Org.apache.commons.beanutils.BeanUtils.copyProperties (Object dest, Object orig)

Spring version: (Reflection mechanism)

Org.springframework.beans.BeanUtils.copyProperties (object source, object target, Class editable, string[] Ignoreproperties)

The new version of spring also integrates the Cglib version: Org.springframework.cglib.beans.BeanCopier

Cglib version: (Use dynamic agent, high efficiency)

Net.sf.cglib.beans.BeanCopier.copy (Object ParamObject1, Object ParamObject2, Converter paramconverter)

Dozer version (XML configuration map, lowest performance):

Org.dozer.DozerBeanMapper.map (Object source, class<t> Destinationclass)

As far as the API is concerned, it doesn't matter which tool class to use, we usually care about two points, functional integrity and performance.

Performance test:

//Source Pojo Packagetf56.lf.lfbase.model.dto;Importjava.io.Serializable;Importtf56.lf.lfbase.model.domain.LfProduct;/*** Classname:productdto * *@version1.0 *@sincev1.0 * @Date June 19, 2017 Yuebiao.wu*/ Public classProductdtoextendsLfproductImplementsserializable{Private Static Final LongSerialversionuid = -1296801547926874402l;}//Target Pojo PackageTf56.lf.lfbase.model.domain;Importjava.io.Serializable;Importjava.util.Date;ImportLombok. Getter;ImportLombok. Setter;/*** classname:product * *@authorTool *@version1.0 *@sincev1.0 * Date June, 2:40:42 PM*/@Getter @setter Public classLfproductImplementsSerializable {Private Static Final LongSerialversionuid = 4054863309367744108L; /*Product ID*/    PrivateString Lfproductid; /*Logistics Financial Account*/    PrivateString Lfaccountid; /*Product Name*/    PrivateString ProductName; /*type of product (Baofu: Guarantee and payment; Miaofu: Second payment; Jishifu: Instant payment;)*/    PrivateString ProductType; /*List of applicable scenarios (comma separated) (COD: Collect payment; FREIGHT: freight;)*/    PrivateString scenetypes; /*Product Description*/    PrivateString ProductDescription; /*Product Rule Description*/    PrivateString productroledescription; /*Loan triggering behavior*/    PrivateString loanaction; /*Collection triggering behavior*/    PrivateString repayaction; /*Product status (enable: enabled; DISABLE: deactivate;)*/    PrivateString Productstatus; /*Warning threshold*/    PrivateLong warnbalance; /*whether the consignor controls the amount (1: yes; 0: no;)*/    PrivateInteger Isconsignercreditlimit; /*whether the carrier controls the amount (1: yes; 0: no;)*/    PrivateInteger Isexpresscreditlimit; PrivateString Productagreementurl; PrivateString Poundagecalctype; PrivateString poundagerate; /*Remark 1*/    PrivateString Remark1; /*operator Number*/    PrivateString Operatorid; /*Enter Date*/    PrivateDate inputdate; /*Record Update Time*/    PrivateDate stampdate; }

Beancopier

Mapper wrapper =NewDozerbeanmapper (); Productdto PDT=Newproductdto (); Pdt.setisconsignercreditlimit (3); Pdt.setloanaction ("323f3"); Pdt.setproductname ("Feiwefe"); Pdt.setpoundagerate ("F332r3"); LongBeg =System.currenttimemillis (); Propertyutilsbean Bean=NewPropertyutilsbean (); Beancopier Copier= Beancopier.create (productdto.class, Lfproduct.class,false);  for(inti=0;i<1000;i++) {lfproduct LFPDT=Newlfproduct ();//try {//try {//bean.copyproperties (LFPDT, PDT);//} catch (Illegalaccessexception | InvocationTargetException e) {//                    //TODO auto-generated Catch block//e.printstacktrace ();//                }//} catch (Nosuchmethodexception e) {//                //TODO auto-generated Catch block//e.printstacktrace ();//            }Copier.copy (PDT, LFPDT,NULL); if(i==3) {//System.out.println (Jacksonhelper.tojson (LFPDT));            }//LFPDT = Wrapper.map (PDT, lfproduct.class);Lfpdt.setinputdate (NewDate ()); } System.out.println (System.currenttimemillis ()-beg);

About 140ms or so.

Mapper wrapper =NewDozerbeanmapper (); Productdto PDT=Newproductdto (); Pdt.setisconsignercreditlimit (3); Pdt.setloanaction ("323f3"); Pdt.setproductname ("Feiwefe"); Pdt.setpoundagerate ("F332r3"); LongBeg =System.currenttimemillis (); Propertyutilsbean Bean=NewPropertyutilsbean (); Beancopier Copier= Beancopier.create (productdto.class, Lfproduct.class,false);  for(inti=0;i<1000;i++) {lfproduct LFPDT=NULL;//try {//try {//bean.copyproperties (LFPDT, PDT);//} catch (Illegalaccessexception | InvocationTargetException e) {//                    //TODO auto-generated Catch block//e.printstacktrace ();//                }//} catch (Nosuchmethodexception e) {//                //TODO auto-generated Catch block//e.printstacktrace ();//            }//copier.copy (PDT, LFPDT, NULL);            if(i==3) {//System.out.println (Jacksonhelper.tojson (LFPDT));} LFPDT= Wrapper.map (PDT, lfproduct.class); Lfpdt.setinputdate (NewDate ()); } System.out.println (System.currenttimemillis ()-beg);

2500ms.

Propertyutilsbean

Mapper wrapper =NewDozerbeanmapper (); Productdto PDT=Newproductdto (); Pdt.setisconsignercreditlimit (3); Pdt.setloanaction ("323f3"); Pdt.setproductname ("Feiwefe"); Pdt.setpoundagerate ("F332r3"); LongBeg =System.currenttimemillis (); Propertyutilsbean Bean=NewPropertyutilsbean (); Beancopier Copier= Beancopier.create (productdto.class, Lfproduct.class,false);  for(inti=0;i<1000;i++) {lfproduct LFPDT=Newlfproduct (); Try {                Try{bean.copyproperties (LFPDT, PDT); } Catch(Illegalaccessexception |invocationtargetexception e) {                    //TODO auto-generated Catch blockE.printstacktrace (); }            } Catch(nosuchmethodexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }//copier.copy (PDT, LFPDT, NULL);            if(i==3) {//System.out.println (Jacksonhelper.tojson (LFPDT));            }//LFPDT = Wrapper.map (PDT, lfproduct.class);Lfpdt.setinputdate (NewDate ()); } System.out.println (System.currenttimemillis ()-beg);

Around 240ms.

So, because most of them are simple copy copies, in this case, Beancopyer>propertyutils>dozer.

Reference:

http://blog.csdn.net/jianhua0902/article/details/8155368

http://blog.csdn.net/liangrui1988/article/details/41802275

Java Bean Attribute Copy tool analogy pair (performance + function)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.