BeanUtils.copyProperties复制不生效 BeanUtils.copyProperties复制不生效的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

BeanUtils.copyProperties复制不生效 BeanUtils.copyProperties复制不生效的解决

蓝风9   2021-09-01 我要评论
想了解BeanUtils.copyProperties复制不生效的解决的相关内容吗,蓝风9在本文为您仔细讲解BeanUtils.copyProperties复制不生效的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:BeanUtils.copyProperties,复制不生效,下面大家一起来学习吧。

前言

呵呵 前端时间使用 BeanUtils.copyProperties 的时候碰到了一个这样的问题

我有两个实体, 有同样的属性, 一个有给定的属性的 getter, 另外一个有 给定的属性的 setter, 但是 我使用 BeanUtils.copyProperties 的时候 把来源对象的这个属性 复制不到 目标对象上面

然后 当时也跟踪了一下代码, 然后 这里整理一下 改代码片段吧

然后在调试的过程中 也发现了一些其他的问题, 呵呵 算是额外的了解吧

一下代码基于 : jdk1.8.0_211 + commons-beanutils 1.9.4

问题的排查

首先来一段测试用例, 里面主要包含了三个类, 一个测试类, 两个实体类

package com.hx.test03;  
import org.apache.commons.beanutils.BeanUtils; 
/**
 * Test24BeanUtilsCopy
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-25 16:55
 */
public class Test24BeanUtilsCopy {
 
  // Test24BeanUtilsCopy
  // 1. 取的 source 的 propertyDescriptor
  // 2. get, set 对应的类型不匹配
  public static void main(String[] args) throws Exception {
 
    Test24ImmutableEntity fromImmutable = new Test24ImmutableEntity("fromImmutable");
    Test24MutableEntity fromMutable = new Test24MutableEntity("fromMutable");
    Test24MutableEntity targetEntity = new Test24MutableEntity("targetEntity");
 
    // does't work
    BeanUtils.copyProperties(targetEntity, fromImmutable);
    System.out.println(targetEntity.getAttr());
    // does't work
    BeanUtils.copyProperties(targetEntity, fromMutable);
    System.out.println(targetEntity.getAttr()); 
  }
}
 
package com.hx.test03; 
/**
 * ImmutablePayment
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-25 16:32
 */
public class Test24ImmutableEntity {
 
  // attr
  private final String attr;
 
  public Test24ImmutableEntity(String attr) {
    this.attr = attr;
  }
 
  public String getAttr() {
    return attr;
  } 
}
package com.hx.test03; 
import java.util.Optional; 
/**
 * ImmutablePayment
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-25 16:32
 */
public class Test24MutableEntity {
 
  // attr
  private String attr;
 
  public Test24MutableEntity(String attr) {
    this.attr = attr;
  }
 
  public Optional<String> getAttr() {
    return Optional.of(attr);
  }
 
//  public String getAttr() {
//    return attr;
//  }
 
  public void setAttr(String attr) {
    this.attr = attr;
  } 
}

以上测试代码输出结果为 :

从测试代码中可以看到这里有两个 BeanUtils.copyProperties 的使用, 并且两个都没有拷贝成功, 我们一个一个的来看

首先是第一个 BeanUtils.copyProperties, 来源对象 和 目标对象分别为 ImmutableEntity 和 MutableEntity

ImmutableEntity 上面有 getAttr, MutableEntity 上面有 setAttr, 但是为什么没有拷贝成功呢 ?

在下图的地方打一个断点 调试一下

调试发现 源对象是可读的, 但是 目标对象不可写?, 为什么呢?, 我们的 MutableEntity 不是有 setAttr 么

在 processPropertyDescriptor 方法之后, 我们发现 attr 属性, 居然不可写了 ?

具体到 processPropertyDescriptor 方法, 他主要干的事情是

// 1. 寻找 getter(存在多个merge) 
// First pass. Find the latest getter method. Merge properties
// of previous getter methods.
 
// 2. 寻找 setter(存在多个merge) 
// Second pass. Find the latest setter method which
// has the same type as the getter method.
 
// 3. merge getter & setter 
// At this stage we should have either PDs or IPDs for the
// representative getters and setters. The order at which the
// property descriptors are determined represent the
// precedence of the property ordering.

以上注释来自于 Introspector.java, 1, 2, 3 的注释来自于我

我们这里重点关注 step2, 需要找到 类型匹配 getter 类型的 setter 方法, 但是我们这里的情况是 getter 返回值是 Optional, setter 返回值是 String, 因此类型不匹配 所以我们上面看到的结果是 有 getter, 没得 setter

实际的上下文信息如下图

以上便是 第一个 BeanUtils.copyProperties 不生效的原因了

第二个 BeanUtils.copyProperties, 原因也是同上, 不过直观的理解来说, attr 是有 getter 并且有 setter 的, 但是 由于规范的约定, 因此 propertyDescriptor 里面有 getter, 没得 setter

问题的扩展

package com.hx.test03;  
import org.apache.commons.beanutils.BeanUtils; 
/**
 * BeanUtilsCopy
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-24 12:49
 */
public class Test23BeanUtilsCopy {
 
  // Test23BeanUtilsCopy
  // 1. 取的 source 的 propertyDescriptor
  // 2. get, set 对应的类型不匹配
  public static void main(String[] args) throws Exception { 
    ImmutableEntity fromImmutable = new ImmutableEntity("fromImmutable");
    MutableEntity fromMutable = new MutableEntity("fromMutable");
    MutableEntity targetEntity = new MutableEntity("targetEntity");
 
    // does't work
    BeanUtils.copyProperties(targetEntity, fromImmutable);
    System.out.println(targetEntity.getAttr());
    // does't work
    BeanUtils.copyProperties(targetEntity, fromMutable);
    System.out.println(targetEntity.getAttr()); 
  }
}
 
/**
 * ImmutablePayment
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-24 12:50
 */
class ImmutableEntity {
  // attr
  private final String attr;
 
  public ImmutableEntity(String attr) {
    this.attr = attr;
  }
 
  public String getAttr() {
    return attr;
  }
}
 
/**
 * MutablePayment
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2020-02-24 12:54
 */
class MutableEntity {
  // attr
  private String attr;
 
  public MutableEntity(String attr) {
    this.attr = attr;
  }
 
//  public Optional<String> getAttr() {
//    return Optional.of(attr);
//  }
  public String getAttr() {
    return attr;
  }
 
  public void setAttr(String attr) {
    this.attr = attr;
  }
}
 

我们吧如上代码 整理到同一个文件中(这其实才是第一个 demo, 上文中的是第二个 demo), 并且调整了 MutableEntity.getter 使其和 setter 的类型能够匹配

但是我们一跑, 发现结果还是有些出人意料

BeanUtilsBean 如下地方打一个断点

我们发现这里有一个奇怪的现象, 源对象不可读, 目标对象不可写??, 这是怎么回事 ?

以 ImmutableEntity. getAttr 为例, 我们在 MethodUtils.getAccessableMethod 里面如下地方打一个断点

我们发现 寻找目标的方法主要有图中 三个地方

第一个是当前类, 另外一个是当前类实现的接口, 另外一个是 当前类的基类(上图还有未截取完的一部分, 限定 method 必须为 public, 否则不允许访问)

  • 1. 在当前类查询 : 首先需要限定当前类是 public(我们这里不满足) public 允许访问
  • 2. 当前类实现的接口查询 : 获取接口以及父接口中 匹配方法名字, 参数列表 的方法
  • 3. 当前类的基类查询 : 获取基类以及更上的基类中, 并且是 public 的基类, 匹配方法名字, 参数列表 的方法

因此, 我们这里的 第二个例子的 两个 BeanUtils.copyProperties 也没有生效

呵呵 不知道这个限定类为 public 的限定是否是 bug 呢?, 还是说 相关规范就是这么约定的呢 ?

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们