Mastering Java Object Copying: Apache vs Spring BeanUtils Explained

This article explains how to copy properties between Java objects, clarifies shallow and deep copy concepts, compares Apache BeanUtils and Spring BeanUtils with code examples, and advises on performance and best‑practice choices for backend development.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Mastering Java Object Copying: Apache vs Spring BeanUtils Explained

Introduction

In Java projects we often need to copy properties between two objects, such as from a data object (DO) to a DTO, without modifying the source. Manually writing getters and setters is tedious, so libraries like Apache BeanUtils, Spring BeanUtils, Dozer, and Orika are used.

Shallow Copy vs Deep Copy

Java distinguishes primitive types and reference types. Assignment of primitives copies the value; assignment of objects copies the reference. Shallow copy copies only primitive fields and copies references for object fields, while deep copy creates a new instance for referenced objects and copies their fields.

Shallow copy : value copy for primitives, reference copy for objects.

Deep copy : value copy for primitives, creates new objects for referenced fields.

Apache BeanUtils

Example classes PersonSource and PersonDest, then using BeanUtils.copyProperties to copy values.

public class PersonSource { private Integer id; private String username; private String password; private Integer age; /* getters/setters omitted */ }
public class PersonDest { private Integer id; private String username; private Integer age; /* getters/setters omitted */ }
public class TestApacheBeanUtils {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        PersonSource personSource = new PersonSource(1, "pjmike", "12345", 21);
        PersonDest personDest = new PersonDest();
        BeanUtils.copyProperties(personDest, personSource);
        System.out.println("persondest: " + personDest);
    }
}
persondest: PersonDest{id=1, username='pjmike', age=21}

The core method is:

public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
    BeanUtilsBean.getInstance().copyProperties(dest, orig);
}

Apache BeanUtils performs a shallow copy and its performance is poor; Alibaba Java coding guidelines discourage its use.

Spring BeanUtils

Similar example using Spring's BeanUtils:

public class TestSpringBeanUtils {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        PersonSource personSource = new PersonSource(1, "pjmike", "12345", 21);
        PersonDest personDest = new PersonDest();
        BeanUtils.copyProperties(personSource, personDest);
        System.out.println("persondest: " + personDest);
    }
}

Spring's implementation checks only matching property names and compatible types, making it safe but requiring identical types for same‑named fields.

private static void copyProperties(Object source, Object target, @Nullable Class<?> editable,
                                   @Nullable String... ignoreProperties) throws BeansException {
    // validation and reflection logic...
}

Conclusion

Apache BeanUtils is slower and not recommended; Spring BeanUtils or other libraries such as CGLIB BeanCopier or Orika provide better performance for object copying.

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.

JavaspringBeanUtilsDeepCopyObjectCopyShallowCopy
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.