zoukankan      html  css  js  c++  java
  • 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进去,并不是深拷贝。

  • 相关阅读:
    齐文词根词缀---3.12、ced-走
    感悟总结---2104012起(文字收集)
    齐文词根词缀---3.11、log说话
    齐文词根词缀---3.10、cata-向下(和cat,cad,cid,cis是一样的)
    齐文词根词缀---3.9、carn-肉、肉欲
    齐文词根词缀---3.8、capit-头(cap帽子)
    齐文词根词缀---3.7、-tect盖子
    齐文词根词缀---3.6、cant-唱、说
    齐文词根词缀---3.5、cad- / cas- 落下(【音变t-d-s】)
    CURL 发送POST请求
  • 原文地址:https://www.cnblogs.com/wanghongsen/p/12558230.html
Copyright © 2011-2022 走看看