读取并遍历spring所有配置,包含系统和自定义(yml或者propreites)
@Autowired
private Environment environment;
public void printAllProperties() {
MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
for (PropertySource<?> propertySource : propertySources) {
// 获取当前 PropertySource 的所有属性名
if (propertySource instanceof EnumerablePropertySource<?>) {
String[] propertyNames = ((EnumerablePropertySource<?>) propertySource).getPropertyNames();
for (String propertyName : propertyNames) {
// 获取属性值
Object propertyValue = propertySource.getProperty(propertyName);
System.out.println(propertyName + ": " + propertyValue);
}
}
}
}
效果