bean之间的复制!BeanUtils.copyProperties、set、BeanCopier还有spring中的BeanUtils.copyProperties之间的区别

  我们一般对进行web开发,在进行对form里的属性值跟实体类复制时,我们大概用到了几种方法,一般常见的set进行复制,
  struts自带的BeanUtils.copyProperties、spring差不多的BeanUtils.copyProperties、还有cglib架包中的BeanCopier,
  如果你使用set进行复制就会感觉到代码的冗长,开发起来不方面,而struts自带的BeanUtils.copyProperties很简洁,直接丢两个
  对象进行返copy,而spring自带的BeanUtils.copyProperties跟strits差不多,但是还是有区别,我们下面考虑到性能的时候就知道了,
  而cglib中的BeanCopier用起来也很简洁,创建一个对象,然后用这个对象的方法进行复制,现在我们讲他们之间的性能
 如果用set,那它是原始的老大,相当快,在大型的数据中进行copy性能是最好的,而struts中的beanUtils.copyproperties那就惨了,
 他由于造BeanCopier是消耗很多性能的, 在执行复杂操作的时候, 最好能现缓存 这个对象。 不然容易发生一样的性能问题,性能是相当的垃圾
 如果你自己的项目不是特别的大对数据量很少,可以使用,而spring中的BeanUtils.copyProperties效率比struts中的是很快的,快几倍,
 但是本人喜欢用cglib中的BeanCopier,他用起来也很简洁,性能跟set差不多,下面就是我测试出来的数据,嘎嘎,希望大家以后也用BeanCopier.

 BeanUtils.copyProperties性能测试

com.wf.form.UserForm uf=(com.wf.form.UserForm)form;
  User u=new User();
  int LOOP_NUMBER=100000;
  Long startTime;
  Long endTime;
  System.out.println(uf.getNAME());
  
  
  startTime=System.currentTimeMillis();
  for(int i=0;i<LOOP_NUMBER;i++){
   BeanUtils.copyProperties(u, uf);
  }
  endTime=System.currentTimeMillis();
  Long BeanUtilstime=endTime-startTime;
 //---------------------------------------------------------- 
  //set性能测试
  startTime=System.currentTimeMillis();
  for(int i=0;i<LOOP_NUMBER;i++){
   u.setNAME(uf.getNAME());
   u.setPWD(uf.getPWD());
   u.setTEL(uf.getTEL());
   u.setDZ(uf.getDZ());
   u.setDEP(uf.getDEP());
  }
  endTime=System.currentTimeMillis();
  Long sgtime=endTime-startTime;
  
 //----------------------------------------------------------------
  //BeanCopier copy=BeanCopier.create性能测试
  BeanCopier copy=BeanCopier.create(UserForm.class, User.class, false);
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  copy.copy(uf, u, null);
 }
 endTime=System.currentTimeMillis();
 Long copiertime=endTime-startTime;
  
 //----------------------------------------------------------------------
 //spring:BeanUtils.copyProperties性能测试
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  org.springframework.beans.BeanUtils.copyProperties( uf,u);
 }
 endTime=System.currentTimeMillis();
 Long springBeanUtilstime=endTime-startTime;
 //-------------------------------------------------------------
 startTime=System.currentTimeMillis();
 for(int i=0;i<LOOP_NUMBER;i++){
  PropertyUtils.copyProperties(u, uf);
 }
 endTime=System.currentTimeMillis(); 
 Long PropertyUtilstime=endTime-startTime;
  
  
  
 
  
  
  System.out.println(u.getNAME());
  
  
  System.out.println("BeanUtilstime............................"+BeanUtilstime+"ms");
  System.out.println("PropertyUtilstime............................"+PropertyUtilstime+"ms");
  System.out.println("settime............................"+sgtime+"ms");
  System.out.println("copiertime............................"+copiertime+"ms");
  System.out.println("springBeanUtilstime............................"+springBeanUtilstime+"ms");


结果:

BeanUtilstime............................937ms
PropertyUtilstime............................3927ms
settime............................16ms
copiertime............................35ms
springBeanUtilstime............................913ms
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值