Spring Boot - inject map from application.yml
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Spring Boot is a powerful framework for building Java-based applications efficiently with minimal setup. One of the key features of Spring Boot is its support for externalized configuration, typically managed via application.yml or application.properties files. This article delves into how to inject a map defined in application.yml into a Spring Boot component or service, enhancing application configurability and manageability.
Understanding Configuration in Spring Boot
Spring Boot supports both .properties and .yml files for external configuration. application.yml is often preferred for complex hierarchical data configurations due to its concise and human-readable structure.
Why Use Maps in Configuration?
Maps are tremendously useful in configuration for:
- Grouping related properties: Manage sets of related configurations as a single logical unit.
- Scalability and organization: Easily scale configurations by simply adding key-value pairs.
- Dynamic configurations: Facilitate dynamic key-value-based configurations without altering the code.
Defining a Map in application.yml
When you want to define a map in application.yml, the configuration syntax is straightforward. Here's how you can define a map:
In the snippet above, a nested map is defined under myapp.settings.feature-toggles, where each feature flag is a key-value pair.
Injecting the Map Using @ConfigurationProperties
The primary way to bind the YAML configuration to a Java class is through the @ConfigurationProperties annotation. It enables the binding of entire classes with prefixed properties.
Step 1: Define a Configuration Class
First, create a Java class that will hold this configuration:
Step 2: Enable Configuration Properties Binding
The @EnableConfigurationProperties annotation is used to ensure that the @ConfigurationProperties beans are effectively managed by Spring Boot:
Step 3: Using the Map in Application Components
Once your map is configured and injected into a @Component, you can utilize it in any Spring-managed component:
In this service, you dynamically check if a feature is enabled based on the map injected from the configuration.
Key Points Summary
Here is a quick summary of the key points:
| Key Point | Description |
| Configuration File | application.yml is used for hierarchical configuration. |
| Define Map | Use nested structures in YAML to define maps. |
| Bindings | Use @ConfigurationProperties with a prefix to bind maps. |
| Enable Bindings | Use @EnableConfigurationProperties to activate configuration property bindings. |
| Inject Configuration | Easily inject configuration maps into services or components using Spring dependency injection. |
| Scalability and Flexibility | Maps offer scalable and flexible configuration handling. |
Additional Considerations
- Validation: Consider using JSR-303 annotations for validating configuration properties. For instance,
@NotEmptycan ensure properties are not undefined. - Profiles: Spring profiles allow managing differing configurations for environments (e.g.,
application-dev.yml,application-prod.yml). - Lombok: Consider using Lombok’s
@Dataor@Getter/@Setterannotations to reduce boilerplate in configuration classes. - Testing: Utilize property-based tests to ensure configurations meet expected criteria. Mock configurations if necessary for isolated tests.
Injecting maps from application.yml provides a powerful and flexible mechanism for managing dynamic application configurations in a Spring Boot application. This modular approach allows developers to maintain clean codebases with external configurations driving application logic.
Related reading
- Spring boot - Service class calling another Service class
- Spring Boot 2 - Unsatisfied dependency on Feign client when autowired for service
- Spring Boot autowire beans from library project
- Spring Boot autowired does not work, classes in different package
- Spring Boot - Limit on number of connections created
- Spring Boot - Loading Initial Data
- Spring boot autowiring an interface with multiple implementations
- Spring Boot can't autowire ConfigurationProperties

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.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.