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性能測試web

 

 

 

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");
  
  
  
  
  
  
  
  spring