温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Bean复制的几种框架有哪些区别

发布时间:2021-10-20 13:59:06 来源:亿速云 阅读:100 作者:iii 栏目:开发技术

本篇内容主要讲解“Bean复制的几种框架有哪些区别”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Bean复制的几种框架有哪些区别”吧!

正文

作为一个员工,最重要的不是编写代码,而是阅读代码,本博主在阅读代码的时候,发现几种实现两个不同类型的Bean的差异,本着研究的精神,仔细对比了Bean复制的性能差异。

比较的框架分别是Apache的BeanUtils,PropertyUtils,Spring的,BeanUtils,Cglib的BeanCopier。

做法是在idea新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:一个FromBean和一个ToBean。

public class FromBean {     private String name;     private int age;     private String address;     private String idno;     private double money;       public double getMoney() {         return money;     }       public void setMoney(double money) {         this.money = money;     }       public String getName() {         return name;     }       public void setName(String name) {         this.name = name;     }       public int getAge() {         return age;     }       public void setAge(int age) {         this.age = age;     }       public String getAddress() {         return address;     }       public void setAddress(String address) {         this.address = address;     }       public String getIdno() {         return idno;     }       public void setIdno(String idno) {         this.idno = idno;     }   }

一个用于测试的BenchmarkTest类

public class BenchmarkTest {     private int count;      public BenchmarkTest(int count) {         this.count = count;         System.out.println("性能测试" + this.count + "==================");     }      public void benchmark(IMethodCallBack m, FromBean frombean) {         try {             long begin = new java.util.Date().getTime();             ToBean tobean = null;             System.out.println(m.getMethodName() + "开始进行测试");             for (int i = 0; i < count; i++) {                  tobean = m.callMethod(frombean);              }             long end = new java.util.Date().getTime();             System.out.println(m.getMethodName() + "耗时" + (end - begin));             System.out.println(tobean.getAddress());             System.out.println(tobean.getAge());             System.out.println(tobean.getIdno());             System.out.println(tobean.getMoney());             System.out.println(tobean.getName());             System.out.println("                                      ");         } catch (Exception e) {             e.printStackTrace();         }     } }

对接口的声明

public interface IMethodCallBack {      String getMethodName();      ToBean callMethod(FromBean frombean)  throws Exception;  }

使用的测试类

public class TestMain {      /**      * @param args      */     public static void main(String[] args) {         FromBean fb = new FromBean();         fb.setAddress("北京市朝阳区大屯路");         fb.setAge(20);         fb.setMoney(30000.111);         fb.setIdno("110330219879208733");         fb.setName("测试");          IMethodCallBack beanutilCB = new IMethodCallBack() {              @Override             public String getMethodName() {                 return "BeanUtil.copyProperties";             }              @Override             public ToBean callMethod(FromBean frombean) throws Exception {                  ToBean toBean = new ToBean();                 BeanUtils.copyProperties(toBean, frombean);                 return toBean;             }         };          IMethodCallBack propertyCB = new IMethodCallBack() {              @Override             public String getMethodName() {                 return "PropertyUtils.copyProperties";             }              @Override             public ToBean callMethod(FromBean frombean) throws Exception {                 ToBean toBean = new ToBean();                 PropertyUtils.copyProperties(toBean, frombean);                 return toBean;             }         };          IMethodCallBack springCB = new IMethodCallBack() {              @Override             public String getMethodName() {                 return "org.springframework.beans.BeanUtils.copyProperties";             }              @Override             public ToBean callMethod(FromBean frombean) throws Exception {                 ToBean toBean = new ToBean();                 org.springframework.beans.BeanUtils.copyProperties(frombean,                         toBean);                 return toBean;             }         };          IMethodCallBack cglibCB = new IMethodCallBack() {             BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class,                     false);              @Override             public String getMethodName() {                 return "BeanCopier.create";             }              @Override             public ToBean callMethod(FromBean frombean) throws Exception {                 ToBean toBean = new ToBean();                 bc.copy(frombean, toBean, null);                 return toBean;             }         };          // 数量较少的时候,测试性能         BenchmarkTest bt = new BenchmarkTest(10);         bt.benchmark(beanutilCB, fb);         bt.benchmark(propertyCB, fb);         bt.benchmark(springCB, fb);         bt.benchmark(cglibCB, fb);          // 测试一万次性能测试         BenchmarkTest bt10000 = new BenchmarkTest(10000);         bt10000.benchmark(beanutilCB, fb);         bt10000.benchmark(propertyCB, fb);         bt10000.benchmark(springCB, fb);         bt10000.benchmark(cglibCB, fb);          // 担心因为顺序问题影响测试结果         BenchmarkTest bt1000R = new BenchmarkTest(10000);         bt1000R.benchmark(cglibCB, fb);         bt1000R.benchmark(springCB, fb);         bt1000R.benchmark(propertyCB, fb);         bt1000R.benchmark(beanutilCB, fb);      }  }

测试的结果如下

Bean复制的几种框架有哪些区别

不过需要注意的是,Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整体的性能还是很好,  从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。

到此,相信大家对“Bean复制的几种框架有哪些区别”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI