获取spring应用中所有的配置信息
前置条件:在一个bean中注入
Autowired
private Environment environment;
获取方法代码:
@GetMapping("obtain.environment")
public R obtainEnvironment() {
StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;
Map<String, Map<String, String>> map = new HashMap<>(8);
Iterator<PropertySource<?>> iterator = standardServletEnvironment.getPropertySources().iterator();
while (iterator.hasNext()) {
PropertySource<?> source = iterator.next();
Map<String, String> m = new HashMap<>(128);
String name = source.getName();
Object o = source.getSource();
if (o instanceof Map) {
for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
String key = entry.getKey();
m.put(key, standardServletEnvironment.getProperty(key));
}
}
map.put(name, m);
}
return R.success(map);
}
网友评论