您的位置:首页 > 运维架构

BeanUtils.copyProperties

2015-08-11 10:33 411 查看
1,首先是谁赋值给谁的问题,先看看源码,网上一大群人目测都没实践过,只会抄别人的;什么先来后付钱,乱扯

private static void copyProperties (Object source , Object target , Class<?> editable, String[] ignoreProperties )
                 throws BeansException {

           Assert. notNull(source, "Source must not be null");
           Assert. notNull(target, "Target must not be null");

           Class<?> actualEditable = target.getClass();
            if ( editable != null) {
                 if (! editable.isInstance( target)) {
                      throw new IllegalArgumentException( "Target class [" + target.getClass().getName() +
                                 "] not assignable to Editable class [" + editable.getName() + "]");
                }
                 actualEditable = editable;
           }
           PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
           List<String> ignoreList = ( ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;

            for (PropertyDescriptor targetPd : targetPds) {
                 if ( targetPd.getWriteMethod() != null &&
                           ( ignoreProperties == null || (!ignoreList.contains( targetPd.getName())))) {
                     PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                      if ( sourcePd != null && sourcePd.getReadMethod() != null) {
                            try {
                                Method readMethod = sourcePd.getReadMethod();
                                 if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                      readMethod.setAccessible( true);
                                }
                                Object value = readMethod.invoke( source);
                                Method writeMethod = targetPd.getWriteMethod();
                                 if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                      writeMethod.setAccessible( true);
                                }
                                 writeMethod.invoke( target, value);
                           }
                            catch (Throwable ex) {
                                 throw new FatalBeanException("Could not copy properties from source to target", ex);
                           }
                     }
                }
           }
     }

}
第一个才是源,第二个才是目标好么,这里就是把第一个的值给第二个,第二个是会被覆盖掉的家伙



2,关于 BeanUtils.copyProperties与 Property.copyProperties的问题,到底是谁能在兼容的类型之间转换。

先说明下,网上的那些啥前者后者的,顺序不分就抄别人的代码的也是醉了

BeanUtils.copyProperties才是能够在一定的范围内进行转换的,同时还要注意一些不能转换时候附带转化过去的默认值null会变成0;

Property.copyProperties则是严格的类型转换

下面是附带摘录的网上一些补充

下面总结一下主要的注意点:

大范围两个工具类都是对两个bean之前存在name相同的属性进行处理,无论是源bean或者目标bean多出的属性均不处理。

具体到BeanUtils是相同name并且类型之间支持转换的属性可以处理,而PropertyUtils不支持类型转换必须是类型和name一样才处理。

对null的处理:PropertyUtils支持为null的场景;BeanUtils对部分属性不支持null的情况,具体为下:

1)、date类型不支持:异常 dateorg.apache.commons.beanutils.ConversionException: No value

specified for 'Date'

2)、Ineger、Boolean、Long等不支持: 转为0;

3)、string:支持,保持null;



关于类型转换的例子:

源bean有属性: private Long dateVal;

目标bean有属性:private Date dateVal;

使用 PropertyUtils,会保错:Caused by: java.lang.IllegalArgumentException: argument type mismatch

使用BeanUtils,则相当于new date(dateVal),网上传言java.util.Date不支持,就测试来说无论是 sql.util 都是ok



对于自定义的对象类型属性 都是浅copy :

比如都有属性:private Base base; Base有一个属性String Test;

new.getBase().setTest("new");

那么old.getBase().getTest()也为new



性能:get,set《PropertyUtils《BeanUtils

BeanUtils的高级功能org.apache.commons.beanutils.Converter接口可以自定义类型之间的转化。PropertyUtils没有。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: