使用Cglib的BeanCopier实现Bean的拷贝

本文介绍如何使用CGLib BeanCopier实现Bean属性之间的高效拷贝,提供了一个静态类用于方便地进行拷贝操作,并详细解释了其与Spring和Apache的BeanUtils等库相比的优势。


      选择Cglib的BeanCopier进行Bean拷贝的理由是,其性能要比Spring的BeanUtils,Apache的BeanUtils和PropertyUtils要好很多,尤其是数据量比较大的情况下。

伦理片 http://www.dotdy.com/

       下面看具体例子:

Java代码   收藏代码
  1. package com.yusj.utils;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import net.sf.cglib.beans.BeanCopier;  
  7.   
  8. /** 
  9.  *  
  10.  * 将beancopier做成静态类,方便拷贝 
  11.  * <br>创建日期:2015年12月1日 
  12.  * <br><b>Copyright 2015 UTOUU All Rights Reserved</b> 
  13.  * @author yushaojian 
  14.  * @since 1.0 
  15.  * @version 1.0 
  16.  */  
  17. public class CglibBeanCopierUtils {  
  18.       
  19.     /** 
  20.      *  
  21.      */  
  22.     public static Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>();  
  23.       
  24.     /**  
  25.     * @Title: copyProperties  
  26.     * @Description: TODO(bean属性转换)  
  27.     * @param source 资源类 
  28.     * @param target  目标类  
  29.     * @author yushaojian 
  30.     * @date 2015年11月25日下午4:56:44 
  31.     */  
  32.     public static void copyProperties(Object source,Object target){  
  33.         String beanKey = generateKey(source.getClass(),target.getClass());  
  34.         BeanCopier copier = null;  
  35.         if (!beanCopierMap.containsKey(beanKey)) {  
  36.             copier = BeanCopier.create(source.getClass(), target.getClass(), false);  
  37.             beanCopierMap.put(beanKey, copier);  
  38.         }else {  
  39.             copier = beanCopierMap.get(beanKey);  
  40.         }  
  41.         copier.copy(source, target, null);  
  42.     }  
  43.     private static String generateKey(Class<?>class1,Class<?>class2){  
  44.         return class1.toString() + class2.toString();  
  45.     }  
  46.     /*注: 
  47.     (1)相同属性名,且类型不匹配时候的处理,ok,但是未满足的属性不拷贝; 
  48.     (2)get和set方法不匹配的处理,创建拷贝的时候报错,无法拷贝任何属性(当且仅当sourceClass的get方法超过set方法时出现) 
  49.     (3)BeanCopier  
  50.     初始化例子:BeanCopier copier = BeanCopier.create(Source.class, Target.class, useConverter=true) 
  51.     第三个参数userConverter,是否开启Convert,默认BeanCopier只会做同名,同类型属性的copier,否则就会报错. 
  52.     copier = BeanCopier.create(source.getClass(), target.getClass(), false); 
  53.     copier.copy(source, target, null); 
  54.     (4)修复beanCopier对set方法强限制的约束 
  55.     改写net.sf.cglib.beans.BeanCopier.Generator.generateClass(ClassVisitor)方法 
  56.     将133行的 
  57.     MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod()); 
  58.     预先存一个names2放入 
  59.      109        Map names2 = new HashMap(); 
  60.      110        for (int i = 0; i < getters.length; ++i) { 
  61.      111          names2.put(setters[i].getName(), getters[i]); 
  62.                 } 
  63.     调用这行代码前判断查询下,如果没有改writeMethod则忽略掉该字段的操作,这样就可以避免异常的发生。*/  
  64.   
  65. }  

 影音先锋电影 http://www.iskdy.com/

maven依赖如下4个包:

Xml代码   收藏代码
  1. <dependency>  
  2.             <groupId>asm</groupId>  
  3.             <artifactId>asm</artifactId>  
  4.             <version>3.3.1</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>asm</groupId>  
  8.             <artifactId>asm-commons</artifactId>  
  9.             <version>3.3.1</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>asm</groupId>  
  13.             <artifactId>asm-util</artifactId>  
  14.             <version>3.3.1</version>  
  15.         </dependency>  
  16.         <dependency>  
  17.             <groupId>cglib</groupId>  
  18.             <artifactId>cglib-nodep</artifactId>  
  19.             <version>2.2.2</version>  
  20.         </dependency>  

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值