How to Use List<String> Environment Variables in Spring Boot's application.yml
1. Introduction
In modern application development, managing configuration settings effectively is crucial. Spring Boot provides a robust way to externalize your configuration using application properties or YAML files. While it's straightforward to define simple variables, you might often need to manage lists, especially when working with multiple settings or multiple values for a single property. In this blog post, we’ll explore how to use a List<String> for environment variables in application.yml
, providing clear examples and real-time use cases to help you master this concept quickly.
2. Usages
Why Use List<String> in Spring Boot?
- Flexible Configuration: Using lists allows you to manage multiple configurations for the same property conveniently. This flexibility is beneficial when dealing with multiple endpoints, roles, or any list-like data.
- Readability: YAML provides a clear, hierarchical format that is easy to read and maintain compared to flat properties files.
- Dynamic Configuration: You can easily change list values based on the environment (development, testing, production) without altering the codebase.
- Strong Typing: Spring automatically converts the values in your list to the correct data type based on your configuration class, ensuring type safety.
3. Code Example
Let’s walk through an example where we manage a list of allowed user roles in a Spring Boot application using application.yml
.
Step 1: Setup Your Spring Boot Project
Assuming you have a Spring Boot project already set up, ensure you have the necessary dependency in your pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Step 2: Define Your application.yml
Next, create your application.yml
to define a list of allowed roles:
app:
allowed-roles:
- USER
- ADMIN
- MANAGER
Step 3: Create a Configuration Properties Class
You will need a class to bind these properties. Create a @ConfigurationProperties
class as follows:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private List<String> allowedRoles;
public List<String> getAllowedRoles() {
return allowedRoles;
}
public void setAllowedRoles(List<String> allowedRoles) {
this.allowedRoles = allowedRoles;
}
}
Step 4: Use the Configuration in Your Service
Now, you can autowire AppConfig
into any service and utilize the allowed roles.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private final AppConfig appConfig;
@Autowired
public UserService(AppConfig appConfig) {
this.appConfig = appConfig;
}
public void printAllowedRoles() {
List<String> roles = appConfig.getAllowedRoles();
System.out.println("Allowed Roles: " + roles);
}
}
4. Explanation
Breakdown of the Example
1. application.yml: We define a list of allowed roles under the app
namespace. YAML syntax allows for easy declaration of lists.
2. AppConfig Class: This class is marked with the @ConfigurationProperties
annotation, indicating that it will be bound to properties from the application.yml
. The prefix = "app"
tells Spring to look for properties under the app
namespace. The defined list will automatically be populated by Spring with the values from the YAML file.
3. UserService Class: This service demonstrates how to access the configuration. The printAllowedRoles
method retrieves the list and displays it. You can expand this logic to enforce role-based access control in your application.
Text-Based Diagram
Below is a simple text-based representation of how the components interact:
application.yml
-----------------
app:
allowed-roles:
- USER
- ADMIN
- MANAGER
|
v
AppConfig Class
-----------------
- allowedRoles: List<String>
- USER
- ADMIN
- MANAGER
|
v
UserService
-----------
- printAllowedRoles()
5. Best Practices
- Validation: Use the
@Validated
annotation on your configuration class to ensure the values meet your expectations (e.g., not empty). - Profiles: Utilize Spring profiles to manage different configurations for different environments, allowing for lists to change without code modifications.
- Documentation: Comment your YAML files clearly. This helps other developers (and your future self) understand the purpose of different settings.
- Immutable Lists: Consider returning an immutable list from your configuration class by using a library like Guava or Java’s
Collections.unmodifiableList()
to prevent accidental modifications. - Testing: Write tests for your configuration properties class to ensure that they load as expected. Use a test configuration to run various scenarios.
6. Conclusion
Working with List<String> environment variables in Spring Boot’s application.yml
can significantly enhance your application's configuration management. By using the approach outlined in this blog, you can easily handle multiple related values, making your applications more flexible and easier to manage. Mastering this concept will empower you to leverage Spring Boot's full capabilities effectively. As you implement this in your projects, you’ll find that managing configurations becomes both easier and more maintainable. Happy coding!