Spring Boot read properties without prefix to a map

孤街浪徒 提交于 2020-01-04 13:38:03

问题


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.

This is my application.properties file

AAPL=25
GDDY=65
test=22

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

回答1:


This can be achieved using the PropertiesLoaderUtils and @PostConstruct

Please check the sample below:

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



回答2:


In spring boot, if you need to get a single value from the application.proprties, you just need to use the @Value annotation with the given name

So to get AAPL value just add a class level property like this

@Value("${AAPL}")
private String aapl;

And if you need to load a full properties file as a map, I'm using the ResourceLoader to load the full file as a stream and then parse it as follows

@Autowired
public loadResources(ResourceLoader resourceLoader) throws Exception {      
      Resource resource = resourceLoader.getResource("classpath:myProperties.properties"));
      BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()));
      String line;
      int pos = 0;
      Map<String, String> map = new HashMap<>();
      while ((line = br.readLine()) != null) {
           pos = line.indexOf("=");
           map.put(line.substring(0, pos), line.substring( pos + 1));
      }
}



回答3:


@PropertySource("classpath:config.properties")
public class GlobalConfig {

  public static String AAPL;
  @Value("${AAPL}")
  private void setDatabaseUrl(String value) {
    AAPL = value;
  }
}

You have to use @Value to get value from application.properties file




回答4:


You can't do it with @ConfigurationProperties as far as I'm aware, those require a prefix to be able to load those properties within the bean.

However, if your goal is to obtain "value Y" for "property X" programmatically, you can always inject Environment and use the getProperty() method to find certain property, for example:

@Configuration
public class InitialConfiguration {
    @Autowired
    private Environment environment;

    @PostConstruct
    public void test() {
        Integer aapl = environment.getProperty("AAPL", Integer.class); // 25
        Integer gddy = environment.getProperty("GDDY", Integer.class); // 65
        Integer test = environment.getProperty("test", Integer.class); // 22
    }
}


来源:https://stackoverflow.com/questions/52251303/spring-boot-read-properties-without-prefix-to-a-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!