Difference between BeanUtils.copyProperties() method and PropertyUtils.copyProperties()

When I merged the branch code of the project into the trunk today, I found that the code that was running normally was wrong on the test server. Later, it was found that when the code was merged, someone replaced BeanUtils with PropertyUtils, which caused the copyProperties method to not automatically convert properties during execution. The data types are analyzed in detail below:

First of all, both come from the same package:

1 import org.apache.commons.beanutils.BeanUtils;
2 import org.apache.commons.beanutils.PropertyUtils;

BeanUtils provides wrappers for java reflection and introspection APIs. Its main purpose is to use the reflection mechanism to process the properties of JavaBean. Because a JavaBean usually contains a large number of attributes, in many cases, the processing of JavaBean results in a large amount of get/set code accumulation, which increases the code length and the difficulty of reading the code. Directly using the object entity copy can increase the code recognition and facilitate maintenance. .

What BeanUtils and PropertyUtils have in common:

1. Assign the value of one object to another object through reflection (provided that the names of the attributes in the objects are the same);

2. CopyProperties(obj1, obj2) of PropertyUtils and BeanUtils; I often get confused and don't know who assigns value to whom. The word "first come, first pay before" helps me remember this function. That is, assign the value of obj2 to obj1;

3. If the instance obj2 in 2 is an empty object, that is, if the value of the new instance is not assigned, the attribute value corresponding to obj1 will also be set to empty;

The difference between the two: BeanUtils and PropertyUtils comparison (compare the copyProperties method here)

The copyProperties() method of PropertyUtils is almost the same as BeanUtils.copyProperties(), the main difference is that the latter provides a type conversion function, that is, when two JavaBeans with the same name are found to be of different types, they are converted within the range of supported data types. PropertyUtils This feature is not supported, so BeanUtils will be faster, more commonly used, and less risky to make mistakes.

For example: create two objects, then assign a value to one object and finally copy the assigned object to another empty object:

 1 public class Person {
 2     private String name;
 3     private String sex;
 4     private String age;
 5     private Date birthday;
 6  
 7     public String getName() {
 8         return name;
 9     }
10  
11     public void setName(String name) {
12         this.name = name;
13     }
14  
15     public String getSex() {
16         return sex;
17     }
18  
19     public void setSex(String sex) {
20         this.sex = sex;
21     }
22  
23     public String getAge() {
24         return age;
25     }
26  
27     public void setAge(String age) {
28         this.age = age;
29     }
30  
31     public Date getBirthday() {
32         return birthday;
33     }
34  
35     public void setBirthday(Date birthday) {
36         this.birthday = birthday;
37     }
38 }
 1 public class Student {
 2     private String name;
 3     private String sex;
 4     private Integer age;
 5     private Date birthday;
 6  
 7     public String getName() {
 8         return name;
 9     }
10  
11     public void setName(String name) {
12         this.name = name;
13     }
14  
15     public String getSex() {
16         return sex;
17     }
18  
19     public void setSex(String sex) {
20         this.sex = sex;
21     }
22  
23     public Integer getAge() {
24         return age;
25     }
26  
27     public void setAge(Integer age) {
28         this.age = age;
29     }
30  
31     public Date getBirthday() {
32         return birthday;
33     }
34  
35     public void setBirthday(Date birthday) {
36         this.birthday = birthday;
37     }
38  
39     @Override
40     public String toString() {
41         return "Student{" +
42                 "name='" + name + '\'' +
43                 ", sex='" + sex + '\'' +
44                 ", age=" + age +
45                 ", birthday=" + birthday +
46                 '}';
47     }
48 }
public class Test {
    public static void main(String[] args) throws Exception{
        Student student= new Student();
        Person person = new Person();
        person.setAge(23);
        person.setName("zhangsan");
        person.setSex("nan");
        person.setBirthday(new Date());
 
        //PropertyUtils.copyProperties(student, person);
        BeanUtils.copyProperties(student, person);
        System.out.println(student.toString());
    }
}
print result:
Student{name='zhangsan', sex='nan', age=23, birthday=Thu Nov 24 10:32:10 CST 20  

When executing BeanUtils.copyProperties(student, person), if the age type of the student object is changed to String, normal results can be printed. But PropertyUtils.copyProperties(student, person) will throw an exception.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325206922&siteId=291194637