springboot 2.0 绑定bean

springboot 2.0 绑定bean

Posted by 郝晓明 on 2019-08-12

springboot 2.0 通过 Binder 绑定bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HashMap<String, String> map = new HashMap(8);
map.put("name", "Abc");
map.put("age", "25");
map.put("address", "chongqing");
map.put("dd", "dd");

Class<User> clazz = (Class<User>) ClassUtils.resolveClassName("com.ai.ecs.test.User", null);

ConfigurationPropertySource propertySource = new MapConfigurationPropertySource(map);
Binder binder = new Binder(propertySource);

BindResult<User> bindResult = binder.bind(ConfigurationPropertyName.EMPTY, Bindable.of(clazz));

if (bindResult.isBound()){
User user = bindResult.get();
System.out.println(JSONObject.toJSONString(user));
}