java开发中beancopy比较

在java应用开发过程中不可避免的会使用到对象copy属性赋值。

1、常用的beancopy工具

组织(包)
工具类
基本原理
其他
apache PropertyUtils java反射  
  BeanUtils java反射  
Spring BeanUtils java反射  
cglib BeanCopier 动态代理 初始化代理类

2、用法举例

  • sourceBean

    public class SourceBean{
     
        public SourceBean(int id,Sting name,String title){
            this.id=id;
        tihs.name=name;
        this.title=title;
    }
        private int id;
        private string name;
        private String tilte;
     
    }
  • dstBean

    public class DstBean{
        private int id;
     
        private string name;
     
        private String tilte;
     
        private String selfFiled;
     
    }
  • 使用方式
public class testBeanCopy{
    DstBean target = new DstBean();
    SourceBean source = new SourceBean(123,"好好学习","天天向上");
    public void testApache(){
        try {
            long start1 = System.currentTimeMillis();
            org.apache.commons.beanutils.PropertyUtils.copyProperties(target, source );
            System.out.println("apache properyUtils--"+ (System.currentTimeMillis()-start1)+"ms");
            System.out.println("target "+target);
 
            start1 = System.currentTimeMillis();
            org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
            System.out.println("apache beanutil--"+ (System.currentTimeMillis()-start1)+"ms");
            System.out.println("target "+target);
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void testSpring(){
        try {
            long start = System.currentTimeMillis();
            BeanUtils.copyProperties(source, target);
            System.out.println("spring--"+(System.currentTimeMillis()-start)+"ms");
            System.out.println("target "+target);
        catch (Exception e) {
            e.printStackTrace();
        }
    }
//------cglib----
private BeanCopier beanCopier = BeanCopier.create(SourceBean.class, DstBean.classfalse);
    public void testCgLib(){
        try {
            long start = System.currentTimeMillis();
            beanCopier.copy(source, target, null);
            System.out.println("cglib--"+(System.currentTimeMillis()-start)+"ms");
            System.out.println("target "+target);
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}  

输出结果,

cglib--0ms
cglib -- target DstBean [id=123, name=好好学习, title=天天向上]
spring--4ms
target DstBean [id=123, name=好好学习, title=天天向上]

apache properyUtils--46ms
target DstBean [id=123, name=好好学习, title=天天向上]
apache beanutil--1ms
target DstBean [id=123, name=好好学习, title=天天向上]

有兴趣的同学可以测试100次、1000次。10000次的结论

特别注意:cglib使用不要每次都创建beancopier,否性能会下降

    1.  
      测试性能,执行10000次

      apache properyUtils–432ms

      spring–309ms
      apache beanutil--232ms
      cglib--3ms
      java copy--2ms

      建议:

      1.如果字段少,使用get/set最快 ---java copy

      2.字段多,调用不频繁,使用apache beanutil,最省事,静态方法拿来即用

      3.字段多,调用频繁,使用cglib,需要创建BeanCopier

原文地址:https://www.cnblogs.com/fanguangdexiaoyuer/p/8358720.html