Read values from YAML file in Spring Boot
This post will discuss how to read values defined in the application.yml file in a Spring Boot application.
In a Spring Boot application, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments. This post will discuss how to read values defined in the application.yml file.
application.yml
Following is a simple YAML file containing demo.environment and demo.hosts properties. The demo.environment is a string type, and demo.hosts is a collection of String.
|
1 2 3 4 5 |
demo: environment: prod hosts: - https://www.example1.com - https://www.example2.com |
Spring Boot provides a method of working with properties that lets strongly typed beans govern and validate the configuration of your application, as shown in the following example:
Config.java
The Following configuration class uses @ConfigurationProperties annotation to bind and validate the external configuration to this class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration @EnableConfigurationProperties @ConfigurationProperties("demo") public class Config { private String environment; private List<String> hosts = new ArrayList<>(); public String getEnvironment() { return environment; } public void setEnvironment(String environment) { this.environment = environment; } public List<String> getHosts() { return hosts; } public void setHosts(List<String> hosts) { this.hosts = hosts; } @Override public String toString() { return "{" + this.getEnvironment() + ", " + this.getHosts() + "}"; } } |
Main.java
Now that all properties are loaded from a YAML file, we create an object of the Config class and display the properties using the CommandLineRunner.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main implements CommandLineRunner { @Autowired private Config config; public static void main(String[] args) { SpringApplication.run(Main.class, args); } @Override public void run(String... strings) throws Exception { System.out.println(config); } } |
Output:
{prod, [https://www.example1.com, https://www.example2.com]}
That’s all about reading values from the YAML file in Spring Boot.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)