Invoking all setters within a class using reflection

后端 未结 4 1840
时光取名叫无心
时光取名叫无心 2020-12-11 10:03

I have a domain object, that for the purposes of this question I will call Person with the following private variables:

String name
int age

4条回答
  •  轮回少年
    2020-12-11 10:25

    I think you could use a library, the Apache Commons BeanUtils. If you have a map that contains field and value pairs, the class PropertyUtils can help you:

    Person person = new Person();
    for(Map.Entry entry : map.entrySet())
        PropertyUtils.setProperty(person, entry.getKey(), entry.getValue());
    

提交回复
热议问题