Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

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.

Further reading:

A Quick Guide to Spring @Value

Learn to use the Spring @Value annotation to configure fields from property files, system properties, etc.

Using Spring @Value With Defaults

A quick and practical guide to setting default values when using the @Value annotation in Spring.

How to Inject a Property Value Into a Class Not Managed by Spring?

Learn how to initialize properties values in Java classes without the direct use of Spring's injection mechanism.

2. Spring YAML File

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.‘

3. Binding YAML to a Config Class

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:

  • @Configuration – this marks the class as a source of bean definitions
  • @ConfigurationProperties – this binds and validates the external configurations to a configuration class
  • @EnableConfigurationProperties – this annotation is used to enable @ConfigurationProperties annotated beans in the Spring application

4. Accessing the YAML Properties

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]

5. YAML Property Overriding

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:

  • Profiles’ properties placed outside the packaged jar
  • Profiles’ properties packaged inside the packaged jar
  • Application properties placed outside the packaged jar
  • Application properties packaged inside the packaged jar

As of Spring Boot 2.4, external files always override packaged files, regardless of whether they’re profile-specific or not.

6. Conclusion

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW