java - Spring Boot 读取没有前缀的属性到 map

标签 java spring spring-boot configurationproperties

我需要在 map 中读取 application.properties 文件中的所有属性 在下面的代码中,属性测试具有相应的值,但 map 是空的。如何在不向属性添加前缀的情况下使用 application.properties 文件中的值填充“映射”。

这是我的 application.properties 文件

AAPL=25
GDDY=65
test=22

我正在像这样使用@ConfigurationProperties

@Configuration
@ConfigurationProperties("")
@PropertySource("classpath:application.properties")
public class InitialConfiguration {
    private HashMap<String, BigInteger> map = new HashMap<>();
    private String test;

    public HashMap<String, BigInteger> getMap() {
        return map;
    }

    public void setMap(HashMap<String, BigInteger> map) {
        this.map = map;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }
}

最佳答案

这可以使用 PropertiesLoaderUtils@PostConstruct

来实现

请检查以下示例:

@Configuration
public class HelloConfiguration {
    private Map<String, String> valueMap = new HashMap<>();
    @PostConstruct
    public void doInit() throws IOException {
        Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
        properties.keySet().forEach(key -> {
            valueMap.put((String) key, properties.getProperty((String) key));
        });
        System.err.println("valueMap -> "+valueMap);
    }
    public Map<String, String> getValueMap() {
        return valueMap;
    }
    public void setValueMap(Map<String, String> valueMap) {
        this.valueMap = valueMap;
    }
}

关于java - Spring Boot 读取没有前缀的属性到 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52251303/

相关文章:

java - 如何通过同一个对象离开迭代器?

java - 如何从驱动程序访问复制构造函数

java - q1,中位数,q3 WEKA java

java - org.hibernate.exception.DataException : could not execute statement

java - Spring登录表单不起作用

java - 使用 SPRING BOOT 时如何在 RequestParams 中保留 ISO 8601 日期格式

javascript - Thymeleaf 模板中 HashMap 的级联下拉列表

java - 如何获取数据库中对象的objectID

grails - 如何使用grails-datastore-gorm在 Spring 启动中获取所有域类?

java - native 镜像的 Spring Boot 构建镜像失败