YAML is used for configuring different profiles for a simple Spring Boot application.
Spring YML file
- Spring profiles help enable Spring Applications to define different properties for different environments.
- The three dashes separating the two profiles indicate the start of a new document so all the profiles can be described in the same YAML file.
Binding YAML to a Config Class
To load a set of related properties from a properties file, we will create a bean class:
- @Configuration marks the class as a source of bean definitions
- @ConfigurationProperties binds and validates the external configurations to a configuration class
- @EnableConfigurationProperties this annotation is used to enable @ConfigurationProperties annotated beans in the Spring application
@Configuration@EnableConfigurationProperties@ConfigurationPropertiespublic class YAMLConfig {privateString name;privateString environment;privateList<String> servers = newArrayList<>();// standard getters and setters}
Accessing the YAML Properties
- You can use get and set methods defined in the binding process.
- Using the get method, you can display the properties
- Using the set method, you can set values for the properties.