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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM