java 递归地将属性从一个 bean 复制到另一个(不是同一个类)(包括嵌套 bean)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29297468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy properties from one bean to another (not the same class) recursively (including nested beans)
提问by Sergey Pauk
Which approach requires the least amount of own written code to achieve a deep copy of one bean to another? The goal is to do it in an automatic way when source and target properties are matched by name.
哪种方法需要最少的自己编写的代码来实现一个 bean 到另一个 bean 的深度复制?目标是在源和目标属性按名称匹配时以自动方式执行此操作。
source main bean:
源主bean:
public class SourceBean {
private String beanField;
private SourceNestedBean nestedBean;
// getters and setters
}
source nested bean:
源嵌套bean:
public class SourceNestedBean {
private String nestedBeanField;
// getters and setters
}
target main bean:
目标主豆:
public class TargetBean {
private String beanField;
private TargetNestedBean nestedBean;
// getters and setters
}
target nested bean:
目标嵌套 bean:
public class TargetNestedBean {
private String nestedBeanField;
// getters and setters
}
Using e.g. Spring BeanUtils.copyProperites()I could create a shallow copy of a SourceBean
to TargetBean
with one line of code but it will not copy nested beans. Is there any mature utility (not necessarily Spring Framework) that would allow to do the deep copy while writing as least own code as possible(pretty much same as BeanUtils.copyProperties())?
使用例如Spring BeanUtils.copyProperites()我可以用一行代码创建一个SourceBean
to的浅拷贝,TargetBean
但它不会复制嵌套的 bean。是否有任何成熟的实用程序(不一定是 Spring 框架)允许在编写尽可能少的自己的代码的同时进行深度复制(与 BeanUtils.copyProperties() 几乎相同)?
回答by Federico Peralta Schaffner
One way to do it is with Hymanson ObjectMapper
, via the convertValue()
method:
一种方法是使用HymansonObjectMapper
,通过convertValue()
方法:
ObjectMapper mapper = new ObjectMapper();
SourceBean source = ...;
TargetBean target = mapper.convertValue(source, TargetBean.class);
Note that convertValue()
is overloaded to also work with generic types. Also beware that convertValue()
will in some circumstances return the value you provided, such as if SourceBean is assignable to TargetBean.
请注意,convertValue()
重载也适用于泛型类型。还要注意,convertValue()
在某些情况下会返回您提供的值,例如 SourceBean 是否可分配给 TargetBean。
As Hymanson is a JSON serialization/deserialization library, what convertValue()
does is serialize source
to JSON in memory, and then deserialize this JSON into an instance of TargetBean
. So high performance is not to be expected. However, conversion is performed with one single line of code.
由于 Hymanson 是一个 JSON 序列化/反序列化库,convertValue()
所以source
在内存中序列化为JSON,然后将这个 JSON 反序列化为TargetBean
. 所以高性能是不可预期的。但是,转换是用一行代码执行的。
If you need performance, the best is to do the mapping manually. Nothing will be better than that.
如果您需要性能,最好是手动进行映射。没有比这更好的了。
If you need simplicity, use Hymanson as explained above.
如果您需要简单,请按照上面的说明使用 Hymanson。
A good trade-off is Orika, a high performance mapper with almost no configuration that doesn't use reflection.
一个很好的权衡是Orika,一种高性能映射器,几乎没有不使用反射的配置。
回答by Kirill Dubovikov
You can use Dozer Mapper to do deep copying. See http://dozer.sourceforge.net/documentation/deepmapping.html
您可以使用 Dozer Mapper 进行深度复制。见http://dozer.sourceforge.net/documentation/deepmapping.html
回答by whjlou
While if you want to use deep copy in Java, you should use ObjectOutputStream and ObjectInputStream and all class you need to copy should implements Serializable.
而如果你想在 Java 中使用深拷贝,你应该使用 ObjectOutputStream 和 ObjectInputStream 并且你需要复制的所有类都应该实现 Serializable。
public Object deepCopy() throws IOException, ClassNotFoundException{
//store object in memory using serial
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
}
回答by cbeutenmueller
Use SerializationUtils from apache commons-lang. And make your object serializable.
使用 apache commons-lang 中的 SerializationUtils。并使您的对象可序列化。