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.
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:
You can inject those values directly into a Spring bean with @Value:
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.
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:
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:
For simple list injection, Spring can often bind directly:
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.
Then register it:
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
@ConfigurationPropertiesover many separate@Valueannotations.
Related reading
- Spring Boot, logback and logging.config property
- Spring Boot logging pattern
- Spring Boot logging with Lombok
- Spring Boot LoggingApplicationListener interfering with Application Server logging
- Spring Boot Maven Plugin Include Resources
- Spring Boot MSSQL Kerberos Authentication
- Spring Boot multi module project with Gradle doesn't build
- Spring Boot Multiple Datasource

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.