BeanUtils.copyProperties()笔记
来源:互联网 发布:c语言的开发环境 编辑:程序博客网 时间:2024/06/05 09:51
最近在学习Spring-boot,视频教学里面有用到这个方法来复制对象的属性。
方法这么多,为什么单独拿这个出来记录呢,因为这个方法可以在不同类型的对象之间复制属性值,看了一下源码,还没有完全看明白,先记录一下。
先上源码:
/** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */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) {Method writeMethod = targetPd.getWriteMethod();if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());if (sourcePd != null) {Method readMethod = sourcePd.getReadMethod();if (readMethod != null &&ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {try {if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {readMethod.setAccessible(true);}Object value = readMethod.invoke(source);if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {writeMethod.setAccessible(true);}writeMethod.invoke(target, value);}catch (Throwable ex) {throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex);}}}}}}
源码还有一部分没有看懂,先记录下暂时的理解:
1.源和目标没有类型的限定的情况下属性名称是否一定要相同?
--属性名称一定要相同,并且类型也要相同,否则无法复制属性.
2.目标对象已经有值的情况下,是否还会把源对象里面的属性值覆盖目标对象的属性值。
--会,即使源对象中某属性的值为Null,目标对象中某属性的值不为null,也会把null值赋值给非null值,所以使用的时候要特别注意,可以先复制,再做一些不需要复制的属性的赋值.
3.如果相同名称的属性值数据类型不一样是否会抛出异常,什么时候回抛出异常?
--如果数据类型能转换,会自动转换,如果不能转换就会报错
阅读全文
0 0
- BeanUtils.copyProperties()笔记
- 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()的用法
- linux下的find文件查找命令与grep文件内容查找命令
- 本机关联域名
- 消息中间件kafka+zookeeper集群部署、测试与应用
- android与本地tomcat的通信,实现登录功能
- 数据结构入门学习系列-6(链表合并与多项式合并算法)
- BeanUtils.copyProperties()笔记
- Linux常用命令之grep
- 讯飞语音:组件未安装(错误码21002)
- Linux命令(压缩,解压tar)
- 占据ADAS市场75%的份额,Mobileye是如何做到的?
- 奥兰多枪击案发生后让你五分钟报平安的Safety Check是怎么实现的?
- IEEE数字感知主席:VR迎来了第二春,但技术仍很多空白
- muduo网络库学习(八)事件驱动循环线程池EventLoopThreadPool
- java的命名规范