Copy properties from one bean to another (not the same class) recursively (including nested beans) [duplicate]

谁说我不能喝 提交于 2019-12-03 17:45:32

问题


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.

source main bean:

public class SourceBean {
    private String beanField;
    private SourceNestedBean nestedBean;

    // getters and setters
}

source nested 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:

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())?


回答1:


One way to do it is with Jackson ObjectMapper, via the convertValue() method:

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.

As Jackson 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.

If you need performance, the best is to do the mapping manually. Nothing will be better than that.

If you need simplicity, use Jackson as explained above.

A good trade-off is Orika, a high performance mapper with almost no configuration that doesn't use reflection.




回答2:


You can use Dozer Mapper to do deep copying. See http://dozer.sourceforge.net/documentation/deepmapping.html




回答3:


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.

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();
}



回答4:


Use SerializationUtils from apache commons-lang. And make your object serializable.



来源:https://stackoverflow.com/questions/29297468/copy-properties-from-one-bean-to-another-not-the-same-class-recursively-inclu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!