Spring Boot
YAML
@Value
Configuration
Java

Spring Boot Load Value from YAML file

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Boot reads configuration from application.yml and makes those values available to your beans through the environment and binding system. For simple values, @Value works well, but for grouped or repeated settings, @ConfigurationProperties is usually the better design.

Load Simple YAML Values With @Value

A typical YAML file might look like this:

yaml
app:
  title: Demo Service
  timeout-seconds: 30

You can inject those values directly into a Spring bean with @Value:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Service;
3
4@Service
5public class AppInfoService {
6
7    @Value("${app.title}")
8    private String title;
9
10    @Value("${app.timeout-seconds}")
11    private int timeoutSeconds;
12
13    public String summary() {
14        return title + " runs with timeout " + timeoutSeconds;
15    }
16}

The important part is the property path syntax. YAML nesting such as app.title becomes the dotted property path ${app.title}.

This is the simplest answer when you need only one or two scalar values.

Constructor Injection Is Cleaner Than Field Injection

@Value also works in constructors, which is usually easier to test and reason about.

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class ClientConfig {
6
7    private final String baseUrl;
8    private final int retries;
9
10    public ClientConfig(
11        @Value("${client.base-url}") String baseUrl,
12        @Value("${client.retries}") int retries
13    ) {
14        this.baseUrl = baseUrl;
15        this.retries = retries;
16    }
17
18    public String getBaseUrl() {
19        return baseUrl;
20    }
21
22    public int getRetries() {
23        return retries;
24    }
25}

This keeps the class immutable and makes the dependencies explicit.

Use Defaults When The Property May Be Missing

You can provide a fallback value directly in the placeholder expression:

java
@Value("${feature.enabled:false}")
private boolean featureEnabled;

If the property is absent, Spring injects false instead of failing startup.

This is useful for optional toggles, but do not overuse it. If a setting is required for correct behavior, failing fast is usually better than silently picking a fallback.

Nested And Collection Values Need More Care

YAML often contains lists or structured sections:

yaml
1app:
2  endpoints:
3    - /api/v1
4    - /api/v2

For simple list injection, Spring can often bind directly:

java
1import java.util.List;
2import org.springframework.beans.factory.annotation.Value;
3import org.springframework.stereotype.Component;
4
5@Component
6public class EndpointRegistry {
7
8    @Value("${app.endpoints}")
9    private List<String> endpoints;
10
11    public List<String> getEndpoints() {
12        return endpoints;
13    }
14}

However, once the structure becomes more complex, @Value gets awkward quickly. String placeholders are not a great long-term API for hierarchical configuration.

Use @ConfigurationProperties For Real Configuration Models

If you have several related settings, bind them into a dedicated type instead of scattering many @Value annotations around the codebase.

java
1import java.util.List;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3
4@ConfigurationProperties(prefix = "app")
5public class AppProperties {
6
7    private String title;
8    private int timeoutSeconds;
9    private List<String> endpoints;
10
11    public String getTitle() {
12        return title;
13    }
14
15    public void setTitle(String title) {
16        this.title = title;
17    }
18
19    public int getTimeoutSeconds() {
20        return timeoutSeconds;
21    }
22
23    public void setTimeoutSeconds(int timeoutSeconds) {
24        this.timeoutSeconds = timeoutSeconds;
25    }
26
27    public List<String> getEndpoints() {
28        return endpoints;
29    }
30
31    public void setEndpoints(List<String> endpoints) {
32        this.endpoints = endpoints;
33    }
34}

Then register it:

java
1import org.springframework.boot.context.properties.EnableConfigurationProperties;
2import org.springframework.context.annotation.Configuration;
3
4@Configuration
5@EnableConfigurationProperties(AppProperties.class)
6public class AppConfig {
7}

This is more maintainable because it gives your configuration a real Java model with type-safe fields.

Profile-Specific YAML Still Works The Same Way

Spring Boot also supports profile sections or separate files such as application-dev.yml. The lookup mechanism is the same from the bean's perspective. Your code still asks for ${app.title} and Spring resolves the active profile's value.

That means the injection style does not change between environments. Only the property source changes.

Common Pitfalls

The most common mistake is using @Value for large nested config trees. It works for one property at a time, but the result becomes brittle and hard to validate.

Another mistake is assuming the YAML path uses slashes or indentation syntax in the placeholder. Spring uses dotted notation such as ${app.title} regardless of how the YAML is indented.

People also forget defaults versus required values. If a property must exist, a silent fallback may hide a bad deployment configuration.

Finally, list and object binding often push people toward stringly typed workarounds. That is usually the signal to switch to @ConfigurationProperties.

Summary

  • Use @Value("${...}") for a small number of simple YAML properties.
  • YAML nesting is referenced with dotted property paths such as ${app.title}.
  • Constructor injection is usually cleaner than field injection.
  • Default values can be supplied inside the placeholder expression.
  • For grouped, nested, or repeated config, prefer @ConfigurationProperties over many separate @Value annotations.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.