Spring的BeanUtils.copyProperties的坑

3 篇文章 0 订阅
1 篇文章 0 订阅

一、问题背景

在做源对象目标对象拷贝时目标对象中继承父类的属性没有成功复制。

二、 Spring 的 BeanUtils.copyProperties 方法

使用 Spring 的 BeanUtils.copyProperties 方法进行属性拷贝时,只会拷贝源对象中定义的属性,而不会拷贝目标对象中继承自父类的属性。因为 BeanUtils.copyProperties() 方法是基于 Java 反射实现的,它只能访问源对象中的属性,无法访问目标对象中继承自父类的属性。

如果需要将源对象中的属性拷贝到目标对象中,包括目标对象中继承自父类的属性,可以使用其他的 Java 对象映射工具,比如 Hutool的 BeanUtil、Apache Commons BeanUtils 和 Dozer 等。这些工具可以通过配置来决定是否拷贝继承自父类的属性。

三、demo

以下是使用 Hutool 中的 BeanUtil.copyProperties() 方法进行属性拷贝的示例代码:

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;

import java.util.*;

public class Example {
    public static void main(String[] args) {
        // 创建一个子类对象
        Child source = new Child();
        source.setPublicField("public field");
        source.setProtectedField("protected field");
        source.setPrivateField("private field");
        source.setDateField(DateUtil.parse("2023-05-22"));
        source.setStringList(CollUtil.newArrayList("a", "b", "c"));
        source.setStringMap(new HashMap<String, String>() {{
            put("key1", "value1");
            put("key2", "value2");
        }});

        // 创建一个父类对象
        Parent target = new Parent();

        // 将子类对象的属性拷贝到父类对象中
        BeanUtil.copyProperties(source, target);

        // 输出父类对象中的属性
        System.out.println("publicField: " + target.getPublicField());
        System.out.println("protectedField: " + target.getProtectedField());
        System.println("privateField: " + target.getPrivateField());
        System.out.println("dateField: " + target.getDateField());
        System.out.println("stringList: " + target.getStringList());
        System.out.println("stringMap: " + target.getStringMap());
    }
}

// 父类
class Parent {
    private String privateField;
    private Date dateField;
    private List<String> stringList;
    private Map<String, String> stringMap;

    public String getPrivateField() {
        return privateField;
    }

    public void setPrivateField(String privateField) {
        this.privateField = privateField;
    }

    public Date getDateField() {
        return dateField;
    }

    public void setDateField(Date dateField) {
        this.dateField = dateField;
    }

    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public Map<String, String> getStringMap() {
        return stringMap;
    }

    public void setStringMap(Map<String, String> stringMap) {
        this.stringMap = stringMap;
    }
}

// 子类
class Child extends Parent {
    public String publicField;
    protected String protectedField;

    public String getPublicField() {
        return publicField;
    }

    public void setPublicField(String publicField) {
        this.publicfield = publicField;
    }

    public String getProtectedField() {
        return protectedField;
    }

    public void setProtectedField(String protectedField) {
        this.protectedField = protectedField;
    }

    private String privateField;

    public String getPrivateField() {
        return privateField;
    }

    public void setPrivateField(String privateField) {
        this.privateField = privateField;
    }
}

输出结果为:

publicField: public field
protectedField: protected field
privateField: private field
dateField: Mon May 22 00:00:00 CST 2023
stringList: [a, b, c]
stringMap: {key1=value1, key2=value2}

四、补充(Spring 的 BeanUtils.copyProperties与hutool中BeanUtil.copyProperties的区别)

Spring 的 BeanUtils.copyProperties() 和 Hutool 中的 BeanUtil.copyProperties() 都是用于对象属性复制的工具方法,但它们在实现细节和使用方式上有一些区别:

1. 底层实现不同

Spring 的 BeanUtils.copyProperties() 方法是基于 Java 反射实现的,它可以将源对象中的属性拷贝到目标对象中,并支持类型转换和自定义转换器。

Hutool 中的 BeanUtil.copyProperties() 方法则是基于 ASM 字节码操作实现的,它不仅可以将源对象中的属性拷贝到目标对象中,还支持自定义映射规则、字段过滤和类型转换。

2. 使用方式不同

Spring 的 BeanUtils.copyProperties() 方法的使用方式如下:

BeanUtils.copyProperties(source, target);

其中,source 是源对象,target 是目标对象。

Hutool 中的 BeanUtil.copyProperties() 方法的使用方式如下:

BeanUtil.copyProperties(source, target, ignoreNullValue);

其中,source 是源对象,target 是目标对象,ignoreNullValue 是一个布尔值,表示是否忽略源对象中值为 null 的属性。如果 ignoreNullValue 为 true,则会忽略源对象中值为 null 的属性,不会拷贝到目标对象中;如果 ignoreNullValue 为 false,则会将源对象中值为 null 的属性拷贝到目标对象中。

3. 支持的类型不同

Spring 的 BeanUtils.copyProperties() 方法支持的类型非常广泛,包括 Java 基本类型、字符串、日期、集合、数组等等。

Hutool 中的 BeanUtil.copyProperties() 方法支持的类型也很广泛,包括 Java 基本类型、字符串、日期、集合、数组等等,但是它还支持一些其他类型,比如枚举、Map、JSONObject 等等。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BeanUtils.copyProperties()是一个常用的Java工具方法,它用于将一个Java对象的属性值复制到另一个Java对象中。该方法属于Apache Commons BeanUtils库,提供了简便的方式来复制对象的属性。 使用BeanUtils.copyProperties()方法,你可以将源对象的属性值复制到目标对象中,前提是这两个对象的属性名和类型相匹配。 以下是使用BeanUtils.copyProperties()方法的示例代码: ```java import org.apache.commons.beanutils.BeanUtils; public class Main { public static void main(String[] args) { SourceObject source = new SourceObject(); source.setFoo("Hello"); DestinationObject destination = new DestinationObject(); try { BeanUtils.copyProperties(destination, source); System.out.println(destination.getFoo()); // 输出:Hello } catch (Exception e) { e.printStackTrace(); } } } class SourceObject { private String foo; public String getFoo() { return foo; } public void setFoo(String foo) { this.foo = foo; } } class DestinationObject { private String foo; public String getFoo() { return foo; } public void setFoo(String foo) { this.foo = foo; } } ``` 在上面的示例中,我们创建了一个源对象source和一个目标对象destination,然后使用BeanUtils.copyProperties()方法将source对象的属性值复制到destination对象中。最后,打印出destination对象的属性值,可以看到它与source对象的属性值相同。 需要注意的是,BeanUtils.copyProperties()方法使用反射机制来实现属性值的复制,因此性能可能不如手动编写复制代码。另外,它只会复制两个对象相同的属性,如果目标对象有额外的属性,不会被赋值。如果需要更灵活的属性复制方式,可以考虑其他库或手动编写代码来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值