获取spring所有propertysources和key-value
Posted On 2017年12月20日
@Test
public void getallProperties() {
System.out.println(a.getEnvironment());
AbstractEnvironment env = (AbstractEnvironment) a.getEnvironment();
if (env.getPropertySources().size() <= 0)
System.out.println("===zero size ====");
// AbstractEnvironment env;
for (Iterator it = env.getPropertySources().iterator(); it.hasNext();) {
Object propertysource = it.next();
if (propertysource instanceof CompositePropertySource) {
System.out.println("================");
CompositePropertySource propertySource = (CompositePropertySource) propertysource;
System.out.println(propertySource.getName());
propertySource.getPropertySources().stream().forEach(property -> {
System.out.println("------------------------");
System.out.print("compositeProperty: " + property);
System.out.println("------------------------");
});
System.out.println("================");
}
else if (propertysource instanceof MapPropertySource) {
MapPropertySource propertySource = (MapPropertySource) propertysource;
System.out.println("================");
System.out.println(propertySource.getName());
String[] properties = propertySource.getPropertyNames();
int size = properties.length;
for (int a = 0; a < size; a++) {
System.out.println("------------------------");
System.out.println(
"mapProperty : " + properties[a] + " = " + propertySource.getProperty(properties[a]));
System.out.println("------------------------");
}
System.out.println("================");
}
else if (propertysource instanceof RandomValuePropertySource) {
RandomValuePropertySource propertySource = (RandomValuePropertySource) propertysource;
System.out.println("randomvalue " + propertySource.toString());
}
else if (propertysource instanceof EndpointWebMvcAutoConfiguration) {
EndpointWebMvcAutoConfiguration propertySource = (EndpointWebMvcAutoConfiguration) propertysource;
System.out.println("EndpointWebMvcAutoConfiguration " + propertySource.toString());
}
else {
System.out.println("other properties");
MapPropertySource propertySource = (MapPropertySource) propertysource;
}
}
}
此篇文章已被阅读2361 次