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的情况,具体为下:
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
- 对于自定义的对象类型属性 都是浅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没有。
- beanUtils.copyProperties
- BeanUtils.copyProperties
- BeanUtils.copyProperties
- BeanUtils.copyProperties()
- BeanUtils.copyProperties
- BeanUtils.copyProperties&&PropertyUtils.copyProperties
- BeanUtils.copyProperties()与PropertyUtils.copyProperties()
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()方法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties() 用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties() 用法
- 关于自执行函数
- [leetcode-97]Interleaving String(java)
- backbonejs快速入门(三
- 使用Attribute注册所有特性
- poj 2226 Muddy Fields 二分图最小点覆盖
- BeanUtils.copyProperties
- android sudio 如何获取sha1与md5值
- IntelliJ 常用快捷键
- 语义分析的一些方法(二)
- 19-集合框架工具类-20-常用对象API(集合框架-JDK5.0特性-ForEach循环)
- EditText属性详情
- mysqldump参数之数据一致性
- Oracle创建用户、角色、授权、建表
- 注册引导页面