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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Using Spring BeanUtils for Object Property Copying and List Conversion

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 <E>   input collection element type
 * @param <T>   output collection element type
 * @return converted collection
 */
public static <E, T> List<T> convertList2List(List<E> input, Class<T> clzz) {
    List<T> 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<User> output = new ArrayList<>();
        List<Employee> 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaspringBeanUtilsObject Mappinglist conversion
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.