许久没有更新过,最近因为这个问题引发线上bug,特再次整理汇总!!!


文章目录

  • 1. 对象拷贝
  • 1.1 引用拷贝
  • 1.2 浅拷贝
  • 1.3 深拷贝
  • 2. 对象拷贝中常用的工具类
  • 2.1 Apache BeanUtils#copyProperties
  • 2.2 SpringUtils#copyProperties
  • 2.3 序列化(JSON)
  • 2.4 MapStruct(推荐)


1. 对象拷贝

Java语言中对象拷贝分为深拷贝和浅拷贝以及对象简单的引用拷贝(也就是通常使用的对象赋值)。

1.1 引用拷贝

引用拷贝即对象的赋值操作,就是通常使用的 obj = new Object() 操作,这种方式不会重新在堆内存开辟一个空间去创建这个对象,只是在栈中新增加了一个引用指向原本的对象。

1.2 浅拷贝

浅拷贝则会在堆内存中重新创建一个对象,但是它内部的属性不会重新去创建,而是直接引用原对象属性的地址。Java语言Object的克隆方法默认是浅拷贝。下面的Student类有个特殊的地方就是它的属性类型都是不可变的,即class类通过final进行了修饰,即使对studentB进行属性的修改,也不会对studentA有什么影响,因为不可变对象每进行修改都是重新创建了一个对象(常量池没有创建只是会进行引用的重新指向)。但是若有个Date类型的属性,那么对studentB Date属性进行修改时就会对原有对象studentA的属性值改变。

// 需要实现Cloneable接口
public class Student implements Cloneable {

    private Long id;
    private String name;
    private Integer age;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class CopyDemo {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student studentA = new Student();
        studentA.setId(1L);
        studentA.setName("tom");
        studentA.setAge(14);
        Student studentB = (Student) studentA.clone();
    }
}

copy到bean对象 java 将map属性 java object copy_Apache

1.3 深拷贝

深拷贝和浅拷贝一样都会重新创建一个对象,和浅拷贝不同的是对象的属性也都是重新去创建的,没有引用原对象属性的地址。要通过clone方法的方式去实现深拷贝必须手动去实现,非不可变对象一定要进行对象的重新创建

package com.tianqb.object.copy;

import java.util.Date;

public class Student implements Cloneable {

    private Long id;
    private String name;
    private Integer age;
    private Date created;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student target = (Student) super.clone();
        if (null != this.created) {
            target.setCreated((Date) this.created.clone());
        }
        return target;
    }
}

2. 对象拷贝中常用的工具类

2.1 Apache BeanUtils#copyProperties

特点:

  • 浅拷贝,属性还是指向原本对象的引用
  • 字段名称相同,类型不同无法进行赋值
  • 基本类型字段和引用对象可以映射

2.2 SpringUtils#copyProperties

特点:

  • 特性同Apache,效率比Apache高
  • 参数位置同Apache不同

2.3 序列化(JSON)

特点:

  • 性能较低,耗时(序列化-反序列化)
  • 深拷贝
  • 基本类型字段和引用对象可以映射
  • 字段名称相同,类型不同可以赋值(如Long -> String)

2.4 MapStruct(推荐)

特点:

  • 灵活(可自主配置字段映射关系)
  • 可以配置多对一映射关系
  • 效率高,准确(编译器代码生成,源码就是get、set方法)
  • 深拷贝

使用:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.3.1.Final</version>
</dependency>
@Mapper
public interface StudentMapper {
 
    StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);
  
    @Mapping(source = "no", target = "number")
    @Mapping(source = "createDate", target = "createDate", dateFormat = "yyyy-MM-dd")
    StudentDO dtoToDo(StudentDTO studentDTO);
}



StudentDO studentDO = StudentMapper.INSTANCE.dtoToDo(studentDTO);

注意点:
由于是编译期间生成get、set代码,假如项目中使用了lombok工具,就会出现编译失败找不到具体的get、set方法的问题,原因是lombok也是编译期间进行代码的生成,但是在mapstruct编译的时候lombok还没有进行代码的生成,因此编译出错,解决的方式如下,需要在maven配置文件中添加两个插件,即lombok和maspstruct。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source> <!-- depending on your project -->
        <target>1.8</target> <!-- depending on your project -->
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.3.1.Final</version>
            </path>
            <!-- other annotation processors -->
        </annotationProcessorPaths>
    </configuration>
</plugin>