Object copy tool class BeanUtils

There are three commonly used object copy methods:

1. Through the set and get methods:

//创建user类
public class User {
    private String name;
    private String age;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String toString(){
        return "name:"+name+",age:"+age;
    }
}
//创建person类
public class Person {
    private String name ;
    private String age;
    private String gender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String toString(){
        return "name:"+name+",age:"+age+",gender:"+gender;
    }
}
//测试类
public class DemoUtil {
    public static void main(String[] args) throws Exception{
        User user = new User();
        user.setAge("15");
        user.setName("Lisi");
        Person person = new Person();
      //通过set,get方法,吧user对象的信息拷贝到person
        person.setAge(user.getAge());
        person.setName(user.getName());
        System.out.println(user.toString());
        System.out.println(person.toString());

    }
}
//输出结果
user=name:Lisi,age:15
person=name:Lisi,age:15,gender:null

2. Through the BeanUtils tool class of apache:

如果属性非常的多,使用set,get方法非常的麻烦。
可以使用工具类来解决:
org.apache.commons.beanutils.BeanUtilsimport
       public class DemoUtil {
       public static void main(String[] args) throws Exception{
        String name ="LiMing";
        String age ="18";
        User user = new User();
        BeanUtils.setProperty(user,"name",name);
        BeanUtils.setProperty(user,"age",age);
        System.out.println("user="+user.toString());
        Person person= new Person();
        BeanUtils.copyProperties(person,user);
        System.out.println("person:"+person.toString());

    }
}

3. Through spring's BeanUtils tool class:

import org.springframework.beans.BeanUtils;//主意导入的是spring的工具类
public class DemoUtil {
    public static void main(String[] args) throws Exception{
        User user = new User();
        user.setAge("17");
        user.setName("zhangSan");
        System.out.println("user="+user.toString());
        Person person= new Person();
        BeanUtils.copyProperties(user,person);
        System.out.println("person:"+person.toString());
        
    }
}
结果:
user=name:zhangSan,age:17
person:name:zhangSan,age:17,gender:null

Summary: From a functional point of view, these three commonly used methods can achieve object copying, but the set and get methods have better performance and are suitable for scenarios with fewer field attributes. The tool class is suitable for the situation where a large number of attributes need to be copied. The performance of spring tool class is worse, and the performance of Apache tool class is the worst. Don't use it unless necessary. However, it should be noted that the position of the parameters of the copyProperties method of spring and apache is exactly the opposite. In the parameters of the copyProperties method of spring, the object to be copied is in the back, and the object to be copied in the parameter of the copyProperties method of apache is in the front. And these two kinds of copy are shallow copy, for those who don't know what is shallow copy and deep copy, Baidu it.

Guess you like

Origin blog.csdn.net/wzs535131/article/details/108270871