我遇到了一个相当困难的问题,假设有一个实体类,代码如下:
class Human {
private Integer age; // age of the human
private String describe; // description of the human based on their age
/**
* Setter method for the age of the human
*
* @param age the age of the human
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* Setter method for the description of the human
* The description is determined based on their age
*
* @param gender the gender of the human
*/
public void setDescribe(String gender) {
String describe = "";
if (this.age < 30) {
describe = "young " + gender;
} else if ( this.age <= 55 && this.age >= 30) {
describe = "middle-aged " + gender;
} else {
describe = "old " + gender;
}
this.describe = describe;
}
}
如代码所示(只是一个示例,属性或类可能是任意的),如果我使用spring并使用spring生成bean,我必须确保首先调用方法setAge。我如何确保这一点?
如果数据库中有一个存储年龄和性别的表,当我使用jpa、mybatis或其他库来反映实体时,如何确保首先调用setAge?
我试着搜索,但没有找到答案。以下是我搜索的关键词和一些相关答案,但似乎并不能解决我的疑问:
Springinitbean,如何确保首先执行setter方法
Spring反射实体类,如何确保先执行setter方法
一个接一个的弹簧呼叫器
当jpa反映实体类setter方法调用顺序时
弹簧设定器方法顺序
Spring-为什么在setter方法之后调用初始化