【发布时间】:2018-02-20 19:40:12
【问题描述】:
我正在使用 Spring,但对它的所有功能并不熟悉。寻找一种将字段从一个 Java 对象实例复制到另一个的好方法。我见过How to copy properties from one Java bean to another?,但我正在寻找更具体的,所以这里是详细信息:
假设我有一些 P 类的两个实例,源和目标,它们有 getter 和 setter a、b、c、d 和其他 20 个。
我想将源的属性复制到目标中,但仅适用于属性名称列表中的所有属性。源或目标中任何属性的值无关紧要。
换句话说,如果列表是 {"a", "b"}
那么我只希望发生以下情况:
P source;
P target;
List<string> properties;
//source, target are populated. properties is {"a", "b"}
//Now I need some Spring (SpEL?) trick to do the equivalent of:
target.setA(source.getA());
target.setB(source.getB());
【问题讨论】: