天天看点

对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀对象拷贝的应用现状简介: 目前流行的较为公用认可的工具类: 原理简介你不知道这些陷阱吧? 一些优化和改进

性能对比: beancopier > propertyutils > beanutils. 其中beancopier的性能高出另外两个100数量级。

业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最安全的做法,但是当数据对象的属性字段数量超过程序员的容忍的程度,代码因此变得臃肿不堪,使用一些方便的对象拷贝工具类将是很好的选择。

apache的两个版本:(反射机制)

org.apache.commons.beanutils.propertyutils.copyproperties(object dest, object orig)

org.apache.commons.beanutils.beanutils.copyproperties(object dest, object orig)

spring版本:(反射机制)

org.springframework.beans.beanutils.copyproperties(object source, object target, class editable, string[] ignoreproperties)

cglib版本:(使用动态代理,效率高)

net.sf.cglib.beans.beancopier.copy(object paramobject1, object paramobject2, converter paramconverter)

都使用静态类调用,最终转化虚拟机中两个单例的工具对象。

public beanutilsbean()

{

  this(new convertutilsbean(), new propertyutilsbean());

}

convertutilsbean可以通过convertutils全局自定义注册。

convertutils.register(new dateconvert(), java.util.date.class);

propertyutilsbean的copyproperties方法实现了拷贝的算法。

1、  动态bean:orig instanceof dynabean:object value = ((dynabean)orig).get(name);然后把value复制到动态bean类

2、  map类型:orig instanceof map:key值逐个拷贝

3、  其他普通类::从beaninfo【每一个对象都有一个缓存的bean信息,包含属性字段等】取出name,然后把sourceclass和targetclass逐个拷贝

copier = beancopier.create(source.getclass(), target.getclass(), false);

copier.copy(source, target, null);

create对象过程:产生sourceclass-》targetclass的拷贝代理类,放入jvm中,所以创建的代理类的时候比较耗时。最好保证这个对象的单例模式,可以参照最后一部分的优化方案。

创建过程:源代码见jdk:net.sf.cglib.beans.beancopier.generator.generateclass(classvisitor)

1、  获取sourceclass的所有public get 方法-》propertydescriptor[] getters

2、  获取targetclass 的所有 public set 方法-》propertydescriptor[] setters

3、  遍历setters的每一个属性,执行4和5

4、  按setters的name生成sourceclass的所有setter方法-》propertydescriptor getter【不符合javabean规范的类将会可能出现空指针异常】

5、  propertydescriptor[] setters-》propertydescriptor setter

6、  将setter和getter名字和类型 配对,生成代理类的拷贝方法。

copy属性过程:调用生成的代理类,代理类的代码和手工操作的代码很类似,效率非常高。

<a href="http://blog.csdn.net/express_wind/article/details/7326359">缺陷预防</a>

陷阱条件

apache- propertyutils

apache- beanutils

spring-  beanutils

cglib-

beancopier

是否可以扩展

useconvete功能

no

yes

yes,但比较难用

(sourceobject,targetobject)的顺序

逆序

ok

对sourceobject特殊属性的限制:(date,bigdecimal等)【见备注1】

no,异常出错

相同属性名,且类型不匹配时候的处理

【见备注2】

异常,拷贝部分属性,非常危险

ok,并能进行初级转换,long和integer互转

异常,拷贝部分属性

ok,但是该属性不拷贝

get和set方法不匹配的处理

【见备注3】

创建拷贝的时候报错,无法拷贝任何属性(当且仅当sourceclass的get方法超过set方法)

对targetobject特殊属性的限制:(date,bigdecimal等)

原因:datetimeconveter的conveter没有对null值的处理

public class errorbeanutilobject { //此处省略getter,setter方法

    private string name;

    private java.util.date date;

 public class errorbeanutilstest {  

    public static void main(string args[]) throws throwable  {  

    errorbeanutilobject from = new errorbeanutilobject(); 

    errorbeanutilobject to = new errorbeanutilobject();  

    //from.setdate(new java.util.date());

    from.setname("tttt");

    org.apache.commons.beanutils.beanutils.copyproperties(to, from);//如果from.setdate去掉,此处出现conveter异常

    system.out.println(tostringbuilder.reflectiontostring(from));

    system.out.println(tostringbuilder.reflectiontostring(to));

    }  

原因:这两个工具类不支持同名异类型的匹配 !!!【包装类long和原始数据类型long是可以的】

public class targetclass {  //此处省略getter,setter方法

    private long num;  

    private string name;

    private long num;

public class errorpropertyutilstest {        

    public static void main(string args[]) throws illegalaccessexception, invocationtargetexception, nosuchmethodexception  {  

        sourceclass from = new sourceclass();  

        from.setnum(1);

        from.setname("name"); 

        targetclass to = new targetclass();  

        org.apache.commons.beanutils.propertyutils.copyproperties(to, from); //抛出参数不匹配异常

        org.springframework.beans.beanutils.copyproperties(from, to);

//抛出参数不匹配异常

        system.out.println(tostringbuilder.reflectiontostring(from));    

        system.out.println(tostringbuilder.reflectiontostring(to));  

public class errorbeancopiertest {    

    /**

     * 从该用例看出beancopier.create的target.class 的每一个get方法必须有队形的set方法

     * @param args

     */

    public static void main(string args[]) {  

        beancopier copier = beancopier.create(unsatifisedbeancopierobject.class, sourceclass.class,false);

        copier = beancopier.create(sourceclass.class, unsatifisedbeancopierobject.class, false); //此处抛出异常创建 

class unsatifisedbeancopierobject {   

    public string getname() {

       return name;

    }

    public void setname(string name) {

       this.name = name;

    public long getnum() {

       return num;

//  public void setnum(long num) {

//     this.num = num;

//  }

<a href="http://blog.csdn.net/express_wind/article/details/7326369">优化方案</a>

public class beanutilsex extends beanutils

  public static void copyproperties(object dest, object orig)

  {

    try

    {

      beanutils.copyproperties(dest, orig);

    } catch (illegalaccessexception ex) {

      ex.printstacktrace();

    } catch (invocationtargetexception ex) {

  }

  static

    convertutils.register(new dateconvert(), java.util.date.class);

    convertutils.register(new dateconvert(), java.sql.date.class);

    convertutils.register(new bigdecimalconvert(), bigdecimal.class);

将beancopier做成静态类,方便拷贝

public class beancopierutils {

     public static map&lt;string,beancopier&gt; beancopiermap = new hashmap&lt;string,beancopier&gt;();

     public static void copyproperties(object source, object target){

         string beankey =  generatekey(source.getclass(), target.getclass());

         beancopier copier =  null;

         if(!beancopiermap.containskey(beankey)){

              copier = beancopier.create(source.getclass(), target.getclass(), false);

              beancopiermap.put(beankey, copier);

         }else{

              copier = beancopiermap.get(beankey);

         }

         copier.copy(source, target, null);

     }   

     private static string generatekey(class&lt;?&gt; class1,class&lt;?&gt;class2){

         return class1.tostring() + class2.tostring();

     }

修复beancopier对set方法强限制的约束

改写net.sf.cglib.beans.beancopier.generator.generateclass(classvisitor)方法

将133行的

methodinfo write = reflectutils.getmethodinfo(setter.getwritemethod());

预先存一个names2放入

/* 109 */       map names2 = new hashmap();

/* 110 */       for (int i = 0; i &lt; getters.length; ++i) {

/* 111 */         names2.put(setters[i].getname(), getters[i]);

/*     */       }

调用这行代码前判断查询下,如果没有改writemethod则忽略掉该字段的操作,这样就可以避免异常的发生。

特别说明:尊重作者的劳动成果,转载请注明出处哦~~~http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp37