spring中BeanUtils.copyProperties()拷贝无法使用super的getter方法获取到值
时间: 2023-07-25 08:20:25 浏览: 72
BeanUtils.copyProperties()方法默认使用JavaBean规范的getter和setter方法来获取和设置属性值。如果您的父类属性的getter方法是使用super关键字来获取值的,那么该方法无法获取到父类属性的值,因为该方法只会查找当前类的getter方法。
解决这个问题的一个方式是在子类中复写父类的getter方法,并在子类的getter方法中使用super关键字来获取父类的属性值。例如:
```java
public class Parent {
private String name;
public String getName() {
return name;
}
}
public class Child extends Parent {
private int age;
@Override
public String getName() {
return super.getName();
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
在这个例子中,Child类复写了Parent类的getName()方法,并在子类的getName()方法中使用super关键字来获取父类的属性值。这样,在调用BeanUtils.copyProperties()方法时,就可以正确地拷贝父类属性的值了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)