日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/19 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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复制失败探究的全部內容,希望文章能夠幫你解決所遇到的問題。

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