
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: March 27, 2025
One of the ways of configuring Spring applications is using YAML configuration files.
In this quick tutorial, we’ll configure different profiles for a simple Spring Boot application using YAML.
Spring profiles help enable Spring Applications to define different properties for different environments.
Let’s take a look at a simple YAML file that contains two profiles. 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.
The relative path of the application.yml file is /myApplication/src/main/resources/application.yml:
spring:
config:
activate:
on-profile: test
name: test-YAML
environment: testing
enabled: false
servers:
- www.abc.test.com
- www.xyz.test.com
---
spring:
config:
activate:
on-profile: prod
name: prod-YAML
environment: production
enabled: true
servers:
- www.abc.com
- www.xyz.com
Note that this setup doesn’t imply that any of these profiles will be active when we start our application. The properties defined in the profile-specific documents won’t be loaded unless we explicitly indicate it; by default, the only active profile will be ‘default.‘
To load a set of related properties from a properties file, we will create a bean class:
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {
private String name;
private String environment;
private boolean enabled;
private List<String> servers = new ArrayList<>();
// standard getters and setters
}
The annotations used here are:
To access the YAML properties, we’ll create an object of the YAMLConfig class, and access the properties using that object.
In the properties file, we’ll set the spring.profiles.active environment variable to prod. If we don’t define this property, only the ‘default’ profile will be active.
The relative path for the properties file is /myApplication/src/main/resources/application.properties:
spring.profiles.active=prod
In this example, we’ll display the properties using the CommandLineRunner:
@SpringBootApplication
public class MyApplication implements CommandLineRunner {
@Autowired
private YAMLConfig myConfig;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.run();
}
public void run(String... args) throws Exception {
System.out.println("using environment: " + myConfig.getEnvironment());
System.out.println("name: " + myConfig.getName());
System.out.println("enabled:" + myConfig.isEnabled());
System.out.println("servers: " + myConfig.getServers());
}
}
The output on the command line:
using environment: production
name: prod-YAML
enabled: true
servers: [www.abc.com, www.xyz.com]
In Spring Boot, YAML files can be overridden by other YAML properties files.
Prior to version 2.4.0, YAML properties were overridden by properties files in the following locations, in order of highest precedence first:
As of Spring Boot 2.4, external files always override packaged files, regardless of whether they’re profile-specific or not.
In this brief article, we learned how to configure properties in Spring Boot applications using YAML. We also discussed the property overriding rules followed by Spring Boot for YAML files.
Follow the Spring Category