Spring Boot
YAML
Configuration
List of Strings
Java

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.

Practice algorithms

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:

yaml
1fruits:
2  - Apple
3  - Banana
4  - Orange

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:

java
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3import java.util.List;
4
5@Component
6public class FruitConfig {
7
8    @Value("${fruits}")
9    private List<String> fruits;
10
11    public List<String> getFruits() {
12        return fruits;
13    }
14}

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:

java
1import org.springframework.boot.context.properties.ConfigurationProperties;
2import org.springframework.stereotype.Component;
3import java.util.List;
4
5@Component
6@ConfigurationProperties(prefix = "myapp")
7public class MyAppConfig {
8
9    private List<String> fruits;
10
11    public List<String> getFruits() {
12        return fruits;
13    }
14
15    public void setFruits(List<String> fruits) {
16        this.fruits = fruits;
17    }
18}

And the corresponding YAML:

yaml
1myapp:
2  fruits:
3    - Apple
4    - Banana
5    - Orange

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.
yaml
1description: |
2  This is a
3  multiline
4  string.
  • Using a greater than > will fold new lines into spaces, suitable for long comments:
yaml
1description: >
2  This is also a
3  multiline string
4  that folds
5  new lines into spaces.

Typing and Validation

Spring Boot allows type-safe configuration via Beans Validation. By utilizing the @Validated and annotations like @NotEmpty, configuration correctness is enforced:

java
1import org.springframework.boot.context.properties.ConfigurationProperties;
2import org.springframework.stereotype.Component;
3import org.springframework.validation.annotation.Validated;
4
5import javax.validation.constraints.NotEmpty;
6import java.util.List;
7
8@Component
9@ConfigurationProperties(prefix = "myapp")
10@Validated
11public class MyAppConfig {
12
13    @NotEmpty
14    private List<String> fruits;
15
16    // Getter and Setter
17}

Table: Summary of YAML List Configuration Methodologies

Configuration MethodSyntax in YAMLAccess in JavaSuitable Use Case
Basic List Definitionyaml&#10;fruits:&#10; - Apple&#10; - Banana&#10; - Orange&#10;@Value("$&#123;fruits&#125;")Simple list of strings without any additional config needs
Nested Property Listyaml&#10;myapp:&#10; fruits:&#10; - Apple&#10; - Banana&#10; - Orange&#10;@ConfigurationPropertiesPart of a larger configuration structure
Multiline Stringyaml&#xA;description: | &#xA; This is a&#xA; multiline&#xA; string.&#xA;@Value("$&#123;description&#125;")Long string requiring multiline readability
ValidationIntegration in @ConfigurationProperties with @Validated and validation constraints in JavaAutomatically validatedWhen 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.