beanutils.copyproperties 深拷贝_properties线程安全吗

beanutils.copyproperties 深拷贝_properties线程安全吗一BeanUtils.copyProperties是什么BeanUtils类全路径为org.springframework.beans.BeanUtils是spring-beans包下的一个用于bean相关工具类。BeanUtils.copyProperties(Objectsource,Objecttarget)这个方法的作用是把source这个bean的全部属性值复制到targe…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

一 BeanUtils.copyProperties是什么

BeanUtils类全路径为org.springframework.beans.BeanUtils是spring-beans包下的一个用于bean相关工具类。

BeanUtils.copyProperties(Object source, Object target)这个方法的作用是 把source这个bean的全部属性值 复制到 target这个bean对象

二 遇到问题BeanUtils.copyProperties(Object source, Object target)写入失败

source和 target 是两个不同类的对象,属性名称全都一样,发现其它字段都拷贝成功,但是有一个字段没有拷贝复制过来

仔细检查发现:该拷贝失败字段的类型不一样,一个是int类型 一个是String类型,

怀疑:source对象和target对象相应属性的名称和类型必须都一样才可以成功拷贝属性值,

经过修改测试发现,亲测有效, 下面阅读源代码进行确认原因。

三 阅读源码

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 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);

}

}

}

}

}

}

spring代码解释说明:

writeMethod 即相关属性的setXX方法,readMethod即 相关属性的getXX方法

ClassUtils.isAssignable(Class> lhsType, Class> rhsType)是否可以转成某个类型,根据返回值 true/false来判断 rhsType 是不是 lhsType

根据代码可以看到,依次遍历target的全部field属性,判断该属性在target中setXX方法的参数类型和 source中getXX方法的返回值类型是否一致,

如果不一致则返回,如果一致则:从source对象中通过getXX得到属性值value,再通过target该属性的set方法,把value值set进去。

四 BeanUtils.copyProperties使用总结

BeanUtils.copyProperties(Object source, Object target)方法,source对象和target对象相应属性的名称和类型必须都一样才可以成功拷贝属性值

BeanUtils.copyProperties只对bean属性进行复制,这里的复制属于浅复制。BeanUtils.copyProperties利用反射,直接将对象的引用set进去,并不是深拷贝。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186397.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号