mardi 7 février 2023

In spring, when the entity class is reflected into a bean, how to specify the calling order of the setter method?

I have encountered a rather difficult problem, Suppose there is an entity class, the code is as follows:

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;
    }
}

As shown in the code (just an example, the attribute or class may be arbitrary), if I use spring and use spring to generate beans, I must ensure that method setAge is called first. How can I ensure this?

If there is a table in the database that stores age and gender, how can I ensure that setAge is called first when I use jpa, mybatis or other libraries to reflect entities?

I tried to search, but I didn't find the answer. The following are the keywords I searched and some related answers, but it doesn't seem to solve my doubts:

Spring init bean, how to ensure that a setter method is executed first

Spring reflection entity class, how to ensure that a setter method is executed first

spring call setter after other setter

When jpa reflects entity classes setter methods call order

Spring setter method order

Spring - why initialization is called after setter method





Aucun commentaire:

Enregistrer un commentaire