Backend Development 5 min read

Understanding Spring BeanUtils.copyProperties: Shallow Copy, Use Cases, and Code Examples

This article explains how Spring's BeanUtils.copyProperties performs a shallow copy, outlines scenarios where it is appropriate, compares it with manual setters, and provides Java code examples illustrating its behavior with parent‑child classes and common pitfalls.

Top Architect
Top Architect
Top Architect
Understanding Spring BeanUtils.copyProperties: Shallow Copy, Use Cases, and Code Examples

The article introduces Spring's BeanUtils.copyProperties method, highlighting that it performs a shallow copy of properties from a source object to a destination object, making it a convenient alternative to manually setting each field.

In typical development, copying properties from a parent class to a subclass can be done either by invoking each setter individually or by using BeanUtils.copyProperties , which is more concise and readable.

It clarifies the difference between shallow and deep copying: a shallow copy only copies references for object‑type fields (the same memory address), whereas a deep copy would duplicate the entire object graph.

The method is suitable when all fields are simple values (e.g., primitives or immutable objects) and no deep copy of nested objects is required.

If a subclass contains nested objects, the decision to use BeanUtils depends on whether those nested objects will be modified; unchanged nested objects can still be safely copied.

Example code demonstrates three classes— Father , Life , and Son (which extends Father )—and shows how BeanUtils.copyProperties copies the fields, including a shared Life instance, resulting in a shallow copy:

@Data
public class Father {
    private String face; // 长相
    private String height; // 身高
    private Life life; // 生命
}
@Data
public class Life {
    private String status;
}
@Data
public class Son extends Father {
    private Life life;

    public static void main(String[] args) {
        Father cuishan = new Father();
        cuishan.setFace("handsome");
        cuishan.setHeight("180");
        Life cuishanLife = new Life();
        cuishanLife.setStatus("alive");
        cuishan.setLife(cuishanLife);
        Son wuji = new Son();
        BeanUtils.copyProperties(cuishan, wuji);
        System.out.println(JSON.toJSONString(cuishan));
        System.out.println(JSON.toJSONString(wuji));
    }
}

Three illustrative cases show the impact of shallow copying:

// case1: both parent and child share the same Life instance; changing parent affects child
// case2: modifying parent after copy may also affect child depending on when the change occurs
// case3: using a new Life instance for the child breaks the shared reference, avoiding side effects

The article also warns about the parameter order difference between Spring's BeanUtils.copyProperties (dest, src) and Apache Commons BeanUtils (src, dest), urging developers to pay attention to avoid bugs.

BackendJavaSpringBeanUtilsCodeExampleShallowCopy
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.