日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

copyproperties爆红_BeanUtils.copyProperties复制失败探究

發布時間:2025/3/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 copyproperties爆红_BeanUtils.copyProperties复制失败探究 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一?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進去,并不是深拷貝。

總結

以上是生活随笔為你收集整理的copyproperties爆红_BeanUtils.copyProperties复制失败探究的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。