Spring Boot YAML configuration for a list of strings
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Spring Boot is a powerful framework that simplifies the development of Java applications by providing a range of features that promote ease of use, speed, and high productivity. One of its strengths is extensive configuration capabilities, especially through YAML files. This allows developers to define beans, configurations, and settings using a concise, human-readable format. In this article, we'll dive into how to configure a list of strings in Spring Boot using YAML, why it's beneficial, and some helpful best practices.
Introduction to YAML in Spring Boot
YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that provides a clean and concise way to define configurations. In Spring Boot, application.yml or application.yaml is used to declare configurations. YAML's hierarchical structure makes it an elegant choice for defining complex configuration properties.
Why Use YAML?
- Readability: YAML files are much easier to read compared to JSON or XML.
- Hierarchical Data: YAML's indentation represents hierarchical data structures, making it simple to read nested properties.
- Support for Various Data Types: YAML supports lists, maps, scalar values, etc.
Configuring a List of Strings
One common requirement is to define lists in the configuration. In YAML, lists are defined using hyphens -.
Basic List of Strings Example
In application.yml, you can define a list of strings as follows:
Here, fruits is the property that holds a list of strings.
Accessing the List in Spring Boot
To access this list in your Spring Boot application, you would typically use the @Value or @ConfigurationProperties annotation.
Using @Value
Here's how to bind the list to a field using @Value:
In this example, the list defined in the YAML file is injected into the fruits field of the FruitConfig component.
Using @ConfigurationProperties
For more complex configurations, or when dealing with multiple related properties, @ConfigurationProperties is a preferred choice:
And the corresponding YAML:
Using @ConfigurationProperties is advantageous as it promotes loose coupling and better maintainability.
Additional Details
Multiline Strings in YAML
YAML allows multiline strings with line breaks, which can enhance readability:
- Using a pipe
|will preserve line breaks.
- Using a greater than
>will fold new lines into spaces, suitable for long comments:
Typing and Validation
Spring Boot allows type-safe configuration via Beans Validation. By utilizing the @Validated and annotations like @NotEmpty, configuration correctness is enforced:
Table: Summary of YAML List Configuration Methodologies
| Configuration Method | Syntax in YAML | Access in Java | Suitable Use Case | |
| Basic List Definition | yaml fruits: - Apple - Banana - Orange | @Value("${fruits}") | Simple list of strings without any additional config needs | |
| Nested Property List | yaml myapp: fruits: - Apple - Banana - Orange | @ConfigurationProperties | Part of a larger configuration structure | |
| Multiline String | yaml
description: | 
 This is a
 multiline
 string.
 | @Value("${description}") | Long string requiring multiline readability | |
| Validation | Integration in @ConfigurationProperties with @Validated and validation constraints in Java | Automatically validated | When requiring validation of configurations |
By using these methodologies in Spring Boot applications, developers can create scalable, maintainable, and robust applications that leverage the flexibility and readability of YAML. YAML configurations promote organized and manageable application settings, crucial for modern software development practices.
Related reading
- Spring Cloud - SQS - The specified queue does not exist for this wsdl version
- Spring cloud SQS - Polling interval
- Spring Kafka Consumer Retry
- Spring Kafka producers throwing TimeoutExceptions
- Spring Cache Cacheable - not working while calling from another method of the same bean
- Spring Cancel Async Task
- Spring RabbitTemplate - How to create queues automatically upon send
- SpringAMQP RabbitMQ how to send directly to Queue without Exchange

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.