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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
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.
