java開發中beancopy比較


在java應用開發過程中不可避免的會使用到對象copy屬性賦值。

1、常用的beancopy工具

組織(包)
工具類
基本原理
其他
apache PropertyUtils java反射  
  BeanUtils java反射  
Spring BeanUtils java反射  
cglib BeanCopier 動態代理 初始化代理類

 

2、用法舉例

  • sourceBean

    public  class  SourceBean{
     
         public  SourceBean( int  id,Sting name,String title){
             this .id=id;
         tihs.name=name;
         this .title=title;
    }
         private  int  id;
         private  string name;
         private  String tilte;
     
    }
  • dstBean

     

    public  class  DstBean{
         private  int  id;
     
         private  string name;
     
         private  String tilte;
     
         private  String selfFiled;
     
    }
  • 使用方式

 

public  class  testBeanCopy{
     DstBean target =  new  DstBean();
     SourceBean source =  new  SourceBean( 123 , "好好學習" , "天天向上" );
     public  void  testApache(){
         try  {
             long  start1 = System.currentTimeMillis();
             org.apache.commons.beanutils.PropertyUtils.copyProperties(target, source );
             System.out.println( "apache properyUtils--" + (System.currentTimeMillis()-start1)+ "ms" );
             System.out.println( "target " +target);
 
             start1 = System.currentTimeMillis();
             org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
             System.out.println( "apache beanutil--" + (System.currentTimeMillis()-start1)+ "ms" );
             System.out.println( "target " +target);
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
 
     public  void  testSpring(){
         try  {
             long  start = System.currentTimeMillis();
             BeanUtils.copyProperties(source, target);
             System.out.println( "spring--" +(System.currentTimeMillis()-start)+ "ms" );
             System.out.println( "target " +target);
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
//------cglib----
private  BeanCopier beanCopier = BeanCopier.create(SourceBean. class , DstBean. class false );
     public  void  testCgLib(){
         try  {
             long  start = System.currentTimeMillis();
             beanCopier.copy(source, target,  null );
             System.out.println( "cglib--" +(System.currentTimeMillis()-start)+ "ms" );
             System.out.println( "target " +target);
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}  

輸出結果,

cglib--0ms
cglib -- target DstBean [id=123, name=好好學習, title=天天向上]
spring--4ms
target DstBean [id=123, name=好好學習, title=天天向上]

apache properyUtils--46ms
target DstBean [id=123, name=好好學習, title=天天向上]
apache beanutil--1ms
target DstBean [id=123, name=好好學習, title=天天向上]

有興趣的同學可以測試100次、1000次。10000次的結論

 

特別注意:cglib使用不要每次都創建beancopier,否性能會下降

 

    1.  
      測試性能,執行10000次

      apache properyUtils–432ms

      spring–309ms
      apache beanutil--232ms
      cglib--3ms
      java copy--2ms

       

      建議:

      1.如果字段少,使用get/set最快 ---java copy

      2.字段多,調用不頻繁,使用apache beanutil,最省事,靜態方法拿來即用

      3.字段多,調用頻繁,使用cglib,需要創建BeanCopier


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM