您好,欢迎访问一九零五行业门户网

详解对象属性复制的三种方法

对象属性复制的三种方法:
1.apache提供的beanutil.copyproperties和propertyutil.copyproperties两种方式
beanutils.copyproperties(转换后的类, 要转换的类); //多一步类型转换,比propertyutils效率还差
propertyutils.copyproperties(转换后的类, 要转换的类);
口诀:后付钱(后付前:后面的复制给前面)会抛异常
2.spring提供的beanutil.copyproperties方式
beanutils.copyproperties(要转换的类, 转换后的类); 
和apache参数顺序相反
3.cglib提供的copy方式
beancopier copy=beancopier.create(要转换的类, 转换后的类, false);
copy.copy(from, to, null);
4.spring提供的copy方式
beancopier copy=beancopier.create(要转换的类, 转换后的类, false);
copy.copy(from, to, null);
1 /**2  * created by hunt on 2017/6/28.3  */4 @data5 public class testfrom {6     private string name;7 }
view code
1 import lombok.data;2 3 /**4  * created by hunt on 2017/6/28.5  */6 @data7 public class testto {8     private string name;9 }
view code
第一种apache方式的beanutils效率测试:
 1 import org.apache.commons.beanutils.beanutils; 2  3 import java.lang.reflect.invocationtargetexception; 4  5 /** 6  * created by hunt on 2017/6/28. 7  */ 8 public class testdemo { 9     public static void main(string[] args) throws invocationtargetexception, illegalaccessexception {10         testfrom testfrom = new testfrom();11         testfrom.setname(hunt);12         testto testto = new testto();13         long begin = system.currenttimemillis();14         for (int i = 0; i <1000000 ; i++) {//一百万次15             beanutils.copyproperties(testto,testfrom);16         }17         long end = system.currenttimemillis();18         long mis = end -begin;19         system.out.println(apache的beanutils.copyproperties耗时 + mis +毫秒);20         system.out.println(testto);21     }22 }
view code
第一种apache方式的propertyutils效率测试:
 1 import org.apache.commons.beanutils.propertyutils; 2  3 import java.lang.reflect.invocationtargetexception; 4  5 /** 6  * created by hunt on 2017/6/28. 7  */ 8 public class testdemo { 9     public static void main(string[] args) throws illegalaccessexception, nosuchmethodexception, invocationtargetexception {10         testfrom testfrom = new testfrom();11         testfrom.setname(hunt);12         testto testto = new testto();13         long begin = system.currenttimemillis();14         for (int i = 0; i <1000000 ; i++) {//一百万次15             propertyutils.copyproperties(testto,testfrom);16         }17         long end = system.currenttimemillis();18         long mis = end -begin;19         system.out.println(apache的propertyutils.copyproperties耗时 + mis +毫秒);20         system.out.println(testto);21     }22 }
view code
第二种spring方式的beanutils效率测试:
 1 import org.springframework.beans.beanutils; 2  3 /** 4  * created by hunt on 2017/6/28. 5  */ 6 public class testdemo { 7     public static void main(string[] args) { 8         testfrom testfrom = new testfrom(); 9         testfrom.setname(hunt);10         testto testto = new testto();11         long begin = system.currenttimemillis();12         for (int i = 0; i <1000000 ; i++) {//一百万次13             beanutils.copyproperties(testfrom,testto);//没抛异常14         }15         long end = system.currenttimemillis();16         long mis = end -begin;17         system.out.println(spring的propertyutils.copyproperties耗时 + mis +毫秒);18         system.out.println(testto);19     }20 }
view code
第三种方式cglib的copy效率测试
 1 import net.sf.cglib.beans.beancopier; 2  3 /** 4  * created by hunt on 2017/6/28. 5  */ 6 public class testdemo { 7     public static void main(string[] args) { 8         testfrom testfrom = new testfrom(); 9         testfrom.setname(hunt);10         testto testto = new testto();11         long begin = system.currenttimemillis();12         for (int i = 0; i <1000000 ; i++) {//一百万次13             beancopier copier = beancopier.create(testfrom.class,testto.class,false);14             copier.copy(testfrom,testto,null);15         }16         long end = system.currenttimemillis();17         long mis = end -begin;18         system.out.println(cglib的copier.copy耗时 + mis +毫秒);19         system.out.println(testto);20     }21 }
view code
第四种方式spring的copy效率测试
 1 import org.springframework.cglib.beans.beancopier; 2  3 /** 4  * created by hunt on 2017/6/28. 5  */ 6 public class testdemo { 7     public static void main(string[] args) { 8         testfrom testfrom = new testfrom(); 9         testfrom.setname(hunt);10         testto testto = new testto();11         long begin = system.currenttimemillis();12         for (int i = 0; i <1000000 ; i++) {//一百万次13             beancopier copier = beancopier.create(testfrom.class,testto.class,false);14             copier.copy(testfrom,testto,null);15         }16         long end = system.currenttimemillis();17         long mis = end -begin;18         system.out.println(spring的copier.copy耗时 + mis +毫秒);19         system.out.println(testto);20     }21 }
view code
总结:这四种方式的效率是由低到高。(注意apache的propertyutils不能进行类型转换的问题)
以上就是详解对象属性复制的三种方法的详细内容。
其它类似信息

推荐信息