1

is it possible to read the yaml file in Spring boot using the @configurationproperties ?

app:
  X1:
    key1: value1
    key2: value2

  X2:
    key1: value3
    key2: value4 
  X3:
    key1: value5
    key2: value6
  X4:
    key1: value7
    key2: value8

I would like to read and put it as Hashmap

webprogrammer
  • 2,393
  • 3
  • 21
  • 27
Rajkumar
  • 11
  • 1

1 Answers1

1

One of the way is to use EnvironmentPostProcessor. You will have to put them in Hahsmap manually in the post-processor. Read the property value as "key:value", then use String split to set them in HashMap.

public class CreatehashMapPostProcessor implements EnvironmentPostProcessor {

  private static final String PROPERTY_SOURCE_NAME = "app.properties";

  @Override
  public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication application) {
    Map<String, Object> map = new HashMap<String, String>();
    String []val = env.getProperty("...");
   map.put(val[0], val[1]);
  }
Dinesh Arora
  • 2,115
  • 3
  • 24
  • 30