Implement a better property copy framework based on asm than spring beanautils


In daily development, it is often necessary to assign the properties of one object to another.

There are many common tools, but they are not concise or powerful enough.

Spring bean utils, which we often use, has good performance, but not enough features.

Bean-Mapping It provides many rich features for daily development.

If you are looking for more extreme performance, you can consider the module implemented by asm, which is about 35% better than spring bean utils.


  • Support shallow copy of object properties

  • Support the assignment of different name fields

  • It supports the conditions of user-defined field property assignment. For example, the assignment can only be performed when the target field is not null

  • Support custom field value conversion, which can be converted to other types or the same type

  • It supports the assignment of attribute fields to object, set and array, which is more convenient.

  • Support high performance replication based on asm

Quick start

get ready

JDK 1.7 and above

Maven 3.X and above

maven project dependency


Core class description


Provides a simple static method copyProperties.

 * Copy properties
 * Assign the value in source to the type with the same name in target that can be assigned. BeanUtils similar to spring.
 * @param source Original object
 * @param target Target object
public static void copyProperties(final Object source, Object target)

Test code reference

See the test code under bean mapping test module for details.

Sample code

Object definition

  • &

The properties of the BaseSource object and the BaseTarget object are the same.

public class BaseSource {

     * name
    private String name;

     * Age
    private int age;

     * Birthday
    private Date birthday;

     * String list
    private List<String> stringList;

    //getter & setter

Property assignment test case

We build the properties of BaseSource and call

BeanUtil.copyProperties(baseSource, baseTarget);

Similar to spring beanautils and Apache beanautils, and verify the results meet our expectations.

     * Basic test
    public void baseTest() {
        BaseSource baseSource = buildBaseSource();
        BaseTarget baseTarget = new BaseTarget();
        BeanUtil.copyProperties(baseSource, baseTarget);

        // The property after assertion assignment is the same as the original
        Assertions.assertEquals(baseSource.getAge(), baseTarget.getAge());
        Assertions.assertEquals(baseSource.getName(), baseTarget.getName());
        Assertions.assertEquals(baseSource.getBirthday(), baseTarget.getBirthday());
        Assertions.assertEquals(baseSource.getStringList(), baseTarget.getStringList());

     * Build user information
     * @return user
    private BaseSource buildBaseSource() {
        BaseSource baseSource = new BaseSource();
        baseSource.setName("Mapping test");
        baseSource.setBirthday(new Date());
        baseSource.setStringList(Arrays.asList("1", "2"));
        return baseSource;

asm replication

brief introduction

Sometimes we have higher performance requirements than convenience requirements.

This framework also provides a replication method based on asm, and its performance is about 35% better than that of spring.

See the benchmark at the end of the article.

maven introduction


Test code

It is similar to beanutil ා copyproperties (object, object), but it does not support annotation and other more rich functions temporarily.

BaseSource baseSource = buildBaseSource();
BaseTarget baseTarget = new BaseTarget();
AsmBeanUtil.copyProperties(baseSource, baseTarget);

Expanding reading

In practice, the situation we encounter will be more complicated than this.

For example, if the names of the two fields are different, we also want to assign values, which is worth dealing with conversion, etc.

Deep learning

Documents related to bean mapping:

01 project module introduction and expected functions

Introduction of 02 bean mapping annotation

03 assignment of different name fields

04 - conditions for validity of custom assignment

05 implementation of custom field conversion

06 support the assignment of attribute fields to object, set and array

07 beanutil - copyproperties (object, class) method


Comparison chart

It can be seen that the performance of the default bean mapping implementation of the framework is general, involving too many features, resulting in slightly lower replication performance than spring.

be based on reflectasm The performance of ASM bean mapping is better than that of spring.

For code examples, see


Compared with the implementation of bean copier, the performance of our implementation still has a lot of room for improvement.

Later, you can consider the implementation of a bean copier like approach.

Open source address

The source code of this framework is all open source, which is also convenient for us to learn.


Keywords: Java Spring Maven Attribute

Added by DapperDanMan on Wed, 18 Dec 2019 10:40:28 +0200