欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot 获取所有属性

程序员文章站 2024-02-13 23:05:10
...
@Autowired
Environment env;

@GetMapping("properties")
public Map<String, Object> allProperties() {
    Map<String, Object> map = new HashMap<>();
    map.put("activeProfiles", env.getActiveProfiles());
    map.put("defaultProfiles", env.getDefaultProfiles());

    MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
    for (PropertySource<?> source : sources) {
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource propertySource = (EnumerablePropertySource) source;
            for (String s : propertySource.getPropertyNames()) {
                map.put(s, propertySource.getProperty(s));
            }
        }
    }

    return map;
}

参考: https://gist.github.com/sandor-nemeth/f6d2899b714e017266cb9cce66bc719d

相关标签: java 开发