BeanUtils.copyProperties

来源:互联网 发布:朝鲜核试验的影响知乎 编辑:程序博客网 时间:2024/05/16 11:31

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

这里的BeanUtils.copyProperties();有多个工具包有,一个是apach的,一个是springmvc的,

apach的copyProperties(target,source),

springmvc的copyProperties(source,target),

所以谁在前面谁在后面,很清楚了,下面的都是介绍的springmvc的源码

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没有。 


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 临时牌驾照丢了怎么办 行驶证年审过期两年怎么办 驾证到期了没换怎么办 在非洲被蚊子咬怎么办 身份证丢了被非法贷款怎么办 未满16岁怎么办身份证 放弃继承权后想反悔怎么办 上海居住证积分中社保断怎么办 换驾驶证但是身份证地址变动怎么办 驾驶证b证扣分了怎么办 c1驾证过期没审怎么办 驾照报名三年过期了怎么办 新车行驶证过期了怎么办 行驶证忘了审怎么办 摩托车驾驶证副本丢了怎么办 人被全险车撞了怎么办 被全险车撞了怎么办 驾驶证和行驶证遗失怎么办 户口迁移身份证没换驾照怎么办 户口迁回时身份证掉了怎么办 户口毕业托管身份证掉了怎么办 身份证和户口都掉了怎么办 毕业生户口迁回原籍没身份证怎么办 落户上海后怎么办医保卡 小车临时牌过期了怎么办 驾驶证地址错了一个字怎么办 驾驶证名下车辆有违章怎么办 a2如果扣12分怎么办 b2一次超速12分怎么办 b2本扣了12分怎么办 驾照扣了27分怎么办 大车一次扣12分怎么办 a2驾照扣10分怎么办 驾驶证强制降级没给驾驶证怎么办 6P升11.2卡了怎么办 b2驾照记满12分怎么办 c1驾驶证没分了怎么办 驾照扣了15分怎么办 驾驶证暂扣六个月后怎么办 醉驾交警不通知怎么办 参军后地方驾驶证年审怎么办