spring map 转class
时间: 2023-09-23 18:12:31 浏览: 69
可以使用Java BeanUtils来实现Spring Map转Class的操作。
示例代码:
```java
import org.apache.commons.beanutils.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 18);
map.put("gender", "男");
Person person = new Person();
BeanUtils.populate(person, map);
System.out.println(person.getName());
System.out.println(person.getAge());
System.out.println(person.getGender());
}
}
class Person {
private String name;
private int age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
运行结果:
```
张三
18
男
```