Spring Boot读取属性而没有前缀到映射 [英] Spring Boot read properties without prefix to a map

查看:256
本文介绍了Spring Boot读取属性而没有前缀到映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在地图中读取application.properties文件中的所有属性 在下面的代码中,属性测试具有相应的值,但映射为空.如何在application.properties文件中填充值"map",而无需在属性中添加前缀.

I need to read all properties in application.properties file in a map In the code below the property test has the respective value but the map is empty. How can I fill "map" with the values in the application.properties file without adding a prefix to the properties.

这是我的application.properties文件

This is my application.properties file

AAPL=25
GDDY=65
test=22

我正在这样使用@ConfigurationProperties

I'm using @ConfigurationProperties like this

@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;
    }
}

这篇关于Spring Boot读取属性而没有前缀到映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆