Home > Java > javaTutorial > body text

Detailed explanation of three methods of object attribute copying

零下一度
Release: 2017-06-29 11:20:59
Original
3335 people have browsed it

Three methods of copying object properties:

1. BeanUtil.copyProperties and PropertyUtil.copyProperties two methods provided by Apache

BeanUtils.copyProperties("Converted class ", "Class to be converted"); //One more step of type conversion, even less efficient than PropertyUtils

 PropertyUtils.copyProperties( "Converted class", "Class to be converted");

## Tip: Pay later (pay later) Front: copy the latter to the front) will throw an exception

2. BeanUtil.copyProperties method provided by spring

BeanUtils.copyProperties(

"Class to be converted ", "Converted class");

Reverse the order of Apache parameters

3. The copy method provided by cglib

BeanCopier copy=BeanCopier.create("Class to be converted"

, "Converted class", false);

copy.copy(from, to,

null);##4. The copy method provided by spring

BeanCopier copy=BeanCopier. create("Class to be converted"

, "Converted class"

, 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 }
Copy after login
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 }
Copy after login
View Code
BeanUtils efficiency test of the first Apache method:

 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 }
Copy after login
View Code

PropertyUtils efficiency test of the first Apache method:

 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 }
Copy after login
View Code

##The second Spring method of BeanUtils efficiency test:

 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 }
Copy after login
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 }
Copy after login
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 }
Copy after login
View Code

 

 

总结:这四种方式的效率是由低到高。(注意Apache的PropertyUtils不能进行类型转换的问题)

The above is the detailed content of Detailed explanation of three methods of object attribute copying. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!