您的位置:首页 > 其它

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

2017-11-03 00:00 459 查看
摘要: 对象属性复制的三种方法:
1.Apache提供的BeanUtil.copyProperties和PropertyUtil.copyProperties两种方式
2.spring提供的BeanUtil.copyProperties方式
3.cglib提供的copy方式
4.spring提供的copy方式

对象属性复制的三种方法:

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

第一种Apache方式的BeanUtils效率测试:

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 }




第一种Apache方式的PropertyUtils效率测试:

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 }




第二种Spring方式的BeanUtils效率测试:

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 }




第三种方式cglib的copy效率测试

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 }




第四种方式Spring的copy效率测试

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 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: