加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

四条使用Spring BeanUtils的总结,避免各种诡异的属性拷贝问题!

发布时间:2019-11-01 04:41:08 所属栏目:建站 来源:绝色天龙
导读:背景 最近项目中在和第三方进行联调一个接口,我们这边发送http请求给对方,然后接收对方的回应,代码都是老代码。根据注释,对方的SDK中写好的Request类有一个无法序列化的bug,所以这边重新写了一个Request类,基本属性都是相同的,但是重点是有一个属性

其实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方法是不行滴。

  1. privatestaticvoid copyProperties(Object source, Object target, @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException { 
  2.         Assert.notNull(source, "Source must not be null"); 
  3.         Assert.notNull(target, "Target must not be null"); 
  4.         Class<?> actualEditable = target.getClass(); 
  5.         if (editable != null) { 
  6.             if (!editable.isInstance(target)) { 
  7.                 throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); 
  8.             } 
  9.  
  10.             actualEditable = editable; 
  11.         } 
  12.  
  13.         PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); 
  14.         List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null; 
  15.         PropertyDescriptor[] var7 = targetPds; 
  16.         int var8 = targetPds.length; 
  17.  
  18.         for(int var9 = 0; var9 < var8; ++var9) { 
  19.             PropertyDescriptor targetPd = var7[var9]; 
  20.             Method writeMethod = targetPd.getWriteMethod(); 
  21.             if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { 
  22.                 PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); 
  23.                 if (sourcePd != null) { 
  24.                     Method readMethod = sourcePd.getReadMethod(); 
  25.                     if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { 
  26.                         try { 
  27.                             if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { 
  28.                                 readMethod.setAccessible(true); 
  29.                             } 
  30.  
  31.                             Object value = readMethod.invoke(source); 
  32.                             if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { 
  33.                                 writeMethod.setAccessible(true); 
  34.                             } 
  35.  
  36.                             writeMethod.invoke(target, value); 
  37.                         } catch (Throwable var15) { 
  38.                             throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", var15); 
  39.                         } 
  40.                     } 
  41.                 } 
  42.             } 
  43.         } 
  44.  
  45.     } 

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读