Using Spring BeanUtils for Object Property Copying and List Conversion
This article demonstrates how to use Spring's BeanUtils.copyProperties method to copy properties between objects and provides a generic utility to convert a list of source objects into a list of target objects, illustrated with Java code examples for User and Employee classes.
The article introduces the org.springframework.beans.BeanUtils.copyProperties method as a convenient way to copy property values from a source object to a target object, avoiding manual getter and setter calls.
/**
* Object property copy
* Copies properties from source object to target object
*
* @param source source object
* @param target target object
*/
public static void copyProperties(Object source, Object target) {
try {
BeanUtils.copyProperties(source, target);
} catch (BeansException e) {
LOGGER.error("BeanUtil property copy failed :BeansException", e);
} catch (Exception e) {
LOGGER.error("BeanUtil property copy failed:Exception", e);
}
}It then presents a generic method to convert a list of objects of type E to a list of objects of type T using the same property‑copying logic.
/**
* @param input input collection
* @param clzz output collection type
* @param
input collection element type
* @param
output collection element type
* @return converted collection
*/
public static
List
convertList2List(List
input, Class
clzz) {
List
output = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(input)) {
for (E source : input) {
T target = BeanUtils.instantiate(clzz);
BeanUtil.copyProperties(source, target);
output.add(target);
}
}
return output;
}Example classes User and Employee are defined, where Employee contains an extra dept field. The article shows how to copy an Employee instance to a User instance and how to convert a list of Employee objects to a list of User objects.
public class User {
private String name;
private Integer age;
// getters, setters, toString()
}
public class Employee {
private String name;
private Integer age;
private String dept;
// constructor, getters, setters, toString()
}A test class demonstrates the usage:
@RunWith(PowerMockRunner.class)
public class TestUtil {
@Test
public void test() {
Employee ee1 = new Employee("A", 21, "it");
Employee ee2 = new Employee("B", 23, "account");
User user = new User();
BeanUtil.copyProperties(ee1, user);
System.out.println(user);
System.out.println("-------------分割线--------------");
List
output = new ArrayList<>();
List
source = Arrays.asList(ee1, ee2);
output = BeanUtil.convertList2List(source, User.class);
for (User str : output) {
System.out.println(str);
}
}
}The article concludes with a brief thank‑you note and references to the original blog source.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.