When to Use Apache vs Spring BeanUtils for Java Object Copying

This article explains the differences between Apache BeanUtils and Spring BeanUtils for copying Java objects, covering shallow vs deep copy concepts, performance drawbacks of Apache's implementation, usage examples, and recommendations for alternative tools like CGLIB BeanCopier and Orika.

Programmer DD
Programmer DD
Programmer DD
When to Use Apache vs Spring BeanUtils for Java Object Copying

Preface

In real project development we often need to copy properties between two object instances, such as from a data object (DO) to a data transfer object (DTO), without modifying the source object. Manually writing many get/set statements is cumbersome, so libraries like Apache BeanUtils, Spring BeanUtils, Dozer, Orika are used.

Object Copy

Before introducing the two BeanUtils, we review basic concepts. Object copying can be shallow or deep.

What are shallow copy and deep copy

In Java, primitive types are copied by value, while object references are copied by reference. Shallow copy copies only primitive values and passes references for objects, so both source and target share the same referenced objects. Deep copy creates a new object for referenced fields and copies their contents.

Shallow copy : copies primitive values and passes object references.

Deep copy : copies primitive values and creates new instances for referenced objects.

BeanUtils

Now we look at the two BeanUtils tools.

Apache BeanUtils

Simple example:

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 most common method is copyProperties(Object dest, Object orig). By default Apache BeanUtils performs a shallow copy. However, its performance is poor, and Alibaba Java Development Guidelines advise against using it.

Ali-Check | Avoid using Apache BeanUtils for property copy.

Apache BeanUtils adds many checks and type conversions, which makes it slow. Its core implementation includes validation, handling of DynaBean, Map, and standard JavaBeans with reflection.

public static void copyProperties(final Object dest, final Object orig)
        throws IllegalAccessException, InvocationTargetException {
    // validation, debug logging, handling DynaBean, Map, and JavaBean...
}

Spring BeanUtils

Example:

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 BeanUtils also uses copyProperties, but its implementation is simpler: it copies matching property names after checking accessibility, and skips ignored or missing properties. It requires the source and target properties to have the same type.

private static void copyProperties(Object source, Object target,
        @Nullable Class<?> editable, @Nullable String... ignoreProperties) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");
    // iterate over target property descriptors, read from source, write to target...
}

Conclusion

Apache BeanUtils has poor performance and is not recommended. Spring BeanUtils is a viable alternative, and other copy frameworks such as CGLIB BeanCopier or Orika (based on Javassist) also provide efficient object mapping.

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.

springApacheBeanUtilsDeepCopyObjectCopy
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.