| 
                         其实list里面的两个类也都是重写的内部类,他们也是不同的,当时他们却顺利copy过去了,为什么呢?因为java的泛型只在编译期起作用,在运行期,list属性就是一个存放Object的集合,在copy后,MixAddRequest的orders属性其实是一个Order类的集合,但却不是自己内部类的集合,是AddRequest的内部类Order的集合,但因为对方是解析json的,所以没有发生错误。。。 
总结 
1. Spring的BeanUtils的CopyProperties方法需要对应的属性有getter和setter方法; 
2. 如果存在属性完全相同的内部类,但是不是同一个内部类,即分别属于各自的内部类,则spring会认为属性不同,不会copy; 
3. 泛型只在编译期起作用,不能依靠泛型来做运行期的限制; 
4. 最后,spring和apache的copy属性的方法源和目的参数的位置正好相反,所以导包和调用的时候都要注意一下。 
最后的最后 
附上spring的源码,getWriteMethod是jdk的方法,会去取set开头的方法,所以没有setter方法是不行滴。 
- privatestaticvoid copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable 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; 
 -         PropertyDescriptor[] var7 = targetPds; 
 -         int var8 = targetPds.length; 
 -  
 -         for(int var9 = 0; var9 < var8; ++var9) { 
 -             PropertyDescriptor targetPd = var7[var9]; 
 -             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 var15) { 
 -                             throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15); 
 -                         } 
 -                     } 
 -                 } 
 -             } 
 -         } 
 -  
 -     } 
 
  
                         (编辑:我爱故事小小网_铜陵站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |