BeanUtils.copyProperties、set、BeanCopier还有spring中的BeanUtils.copyProperties之间的区别

来源:互联网 发布:linux 查看cpu信息 编辑:程序博客网 时间:2024/06/07 01:26

 /我们一般对进行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(u, uf);
 }
 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");