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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章