BeanUtils.copyProperties
时间: 2023-10-12 09:16:01 浏览: 97
BeanUtils.copyProperties is a method provided by Apache Commons BeanUtils library which can be used to copy property values between Java Beans.
The method signature is as follows:
```
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException
```
Here, `dest` is the destination object and `orig` is the source object from which properties need to be copied. The method copies the values of all the properties from the source object to the destination object, provided that the property names and types match on both the objects.
For example, consider two Java Beans `Person` and `Employee` as follows:
```
public class Person {
private String name;
private int age;
// getters and setters
}
public class Employee {
private String name;
private int age;
private String designation;
// getters and setters
}
```
Now, if we want to copy the properties from a `Person` object to an `Employee` object, we can use the BeanUtils.copyProperties method as follows:
```
Person person = new Person();
person.setName("John");
person.setAge(30);
Employee employee = new Employee();
BeanUtils.copyProperties(employee, person);
```
After executing the above code, the `employee` object will have the `name` and `age` properties set to the values from the `person` object. The `designation` property will remain unchanged as it does not exist in the `person` object.
阅读全文