Spring中BeanUtils.copyProperties的坑及解决
一、问题背景
在做 源对象 与 目标对象 拷贝时目标对象中继承父类的属性没有成功复制。
二、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 等等。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
在springboot中如何集成clickhouse进行读写操作
本文介绍了在Spring Boot中集成ClickHouse的步骤,包括引入依赖、配置数据源、编写实体类和Mapper类进行CRUD操作,特别提到批量插入时需要在SQL语句中添加`FORMAT`以避免错误,在实际应用中,与MySQL的操作类似,只需将ClickHouse当作MySQL使用2024-11-11
Java concurrency之AtomicReference原子类_动力节点Java学院整理
AtomicReference是作用是对"对象"进行原子操作。这篇文章主要介绍了Java concurrency之AtomicReference原子类,需要的朋友可以参考下2017-06-06
Springboot配置全局跨域未生效,访问接口报错问题及解决
这篇文章主要介绍了Springboot配置全局跨域未生效,访问接口报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12
springboot启动报错:application startup failed问题
这篇文章主要介绍了springboot启动报错:application startup failed问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-07-07


最新评论