How to pass a MapString, String with application.properties
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Passing a Map<String, String>
with application.properties
in Spring Boot applications can simplify configurations when you want to manage multiple related values together. This article explains how to configure a Map<String, String>
using application.properties
and access it in a Spring Boot application.
Basics of application.properties
The application.properties
file is a key-value pair-based configuration file. It is a central configuration file in Spring Boot applications often used to externalize configuration, allowing you to configure application settings distinct from application logic.
Defining a Map in application.properties
When you want to configure a Map<String, String>
, you typically represent it in a flat structure, where keys represent the final map key paths. Here's a basic example of how you might define a map called my.map
:
- Prefix: The
@ConfigurationPropertiesannotation with theprefixattribute specifies that the properties starting withmyshould be bound to this component. - Map Binding: Spring Boot automatically binds the properties
my.map.key1,my.map.key2, etc., to themapfield in theMyPropertiesclass. - Injection:
MyPropertiesis injected into the controller using Spring's@Autowiredannotation. - Access: You can access the map through its getter method, enabling you to utilize it within your application seamlessly.
- Nested Properties: If you require nested properties and maps, you can adopt a more complex structure of nested classes corresponding to nested property paths.
- Profiles: Consider using Spring Profiles to manage environment-specific properties for maps. This facilitates different property values for development, testing, and production environments.
Related reading
- How to pass an ArrayList to a varargs method parameter?
- How to pass JVM options from bootRun
- How to pass multiple bootstrap servers for listener using spring-kafka
- How to pass system property to Gradle task
- How to pass TTL in Cassandra Java Driver QueryBuilder?
- How to pause / sleep thread or process in Android?
- how to pause and resume @KafkaListener using spring-kafka
- How to perform an action only if the Mono is empty and throw an error if not empty

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.