Skip to content

Instantly share code, notes, and snippets.

@sdhjl2000
Created November 9, 2017 00:44
Show Gist options
  • Save sdhjl2000/062c4e63efe2560147063074f5fe9765 to your computer and use it in GitHub Desktop.
Save sdhjl2000/062c4e63efe2560147063074f5fe9765 to your computer and use it in GitHub Desktop.
fetchAllProperties
private Properties fetchAllProperties() {
final Properties properties = new Properties();
for (Iterator it = ((AbstractEnvironment) environment).getPropertySources().iterator(); it.hasNext(); ) {
PropertySource propertySource = (PropertySource) it.next();
if (propertySource instanceof EnumerablePropertySource) {
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
properties.put(key, propertySource.getProperty(key));
}
}
if (propertySource instanceof PropertiesPropertySource) {
properties.putAll(((MapPropertySource) propertySource).getSource());
}
if (propertySource instanceof CompositePropertySource) {
properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
}
}
return properties;
}
private Properties getPropertiesInCompositePropertySource(CompositePropertySource compositePropertySource) {
final Properties properties = new Properties();
compositePropertySource.getPropertySources().forEach(propertySource -> {
if (propertySource instanceof EnumerablePropertySource) {
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
properties.put(key, propertySource.getProperty(key));
}
}
if (propertySource instanceof MapPropertySource) {
properties.putAll(((MapPropertySource) propertySource).getSource());
}
if (propertySource instanceof CompositePropertySource)
properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
});
return properties;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment