Search code examples
spring-bootyamlconfigurationproperties

Load Yml at runtime with Spring Boot


I have multiple yml files in different folders. All the files in the folder share the same property structure which I mapped with a java bean.

At runtime, with a factory, I want to get the right bean populated with the values of the specific file chosen at runtime. How do I do that?

Thanks


Solution

  • The @ConfigurationProperties annotation or the mechanism behind it is built to be used for configuration of an application at startup, not loading data at runtime.

    I'm sure you could somehow start mini spring environments at runtime just to read this data using different spring profiles (this is e.g. how spring-cloud-configserver loads properties) but this seems not right and there are better alternatives.

    E.g., if you need that data to be loaded at runtime, you can use jackson's yamlfactory for that, with that you can read your data in 3-4 statements. A good example is here: https://www.baeldung.com/jackson-yaml.