Spring Boot
application.properties
Map<String
String>
Java
configuration

How to pass a MapString, String with application.properties

Interview Questions practice on Codemia

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

Browse interview questions

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 @ConfigurationProperties annotation with the prefix attribute specifies that the properties starting with my should be bound to this component.
  • Map Binding: Spring Boot automatically binds the properties my.map.key1 , my.map.key2 , etc., to the map field in the MyProperties class.
  • Injection: MyProperties is injected into the controller using Spring's @Autowired annotation.
  • 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
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.