Loading Lists from Properties Files with Spring @Value Annotation and Spring EL

Nov 16, 2025 · Programming · 11 views · 7.8

Keywords: Spring Framework | Property Configuration | List Loading | Spring EL | @Value Annotation

Abstract: This technical paper comprehensively explores how to load list-type configurations from .properties files using Spring's @Value annotation and Spring Expression Language (Spring EL). Through detailed analysis of core implementation principles, code examples, and best practices, it demonstrates automatic conversion from properties to List without custom code, while comparing differences between XML and properties file configurations. The paper also provides in-depth examination of Spring Boot's externalized configuration mechanisms and property binding strategies.

Introduction and Background

In modern Spring application development, externalized configuration management is crucial for building maintainable and scalable applications. Property files, as the most commonly used configuration carriers, are widely recognized for their flexibility and ease of use. However, when configuration items involve complex data structures such as lists, developers often face challenges in elegantly mapping property values to Java collection objects.

While traditional XML configuration supports complete collection definitions, it lacks the simplicity and environment adaptability of property files. This paper focuses on solving this practical problem by leveraging the powerful capabilities of Spring Expression Language to achieve automatic conversion from comma-separated strings in property files to Java Lists.

Core Solution: Spring EL Expressions

Spring Expression Language (Spring EL) provides powerful processing capabilities for property injection. For list-type configurations, we can utilize string splitting operations to achieve automatic conversion from property values to collections.

The basic implementation code is shown below:

@Value("#{'${my.list.of.strings}'.split(',')}")
private List<String> myList;

This code snippet demonstrates a core application scenario of Spring EL. The expression #{'${my.list.of.strings}'.split(',')} first resolves the property placeholder through ${...} to obtain the original comma-separated string, then calls the split(',') method for string splitting, ultimately generating a standard Java List object.

Property File Configuration Standards

To ensure correct configuration loading, property files need to follow specific format standards. List-type configurations should use comma-separated string format:

my.list.of.strings=ABC,CDE,EFG

This format maintains the simplicity of property files while providing sufficient information density. In actual projects, it is recommended to adopt meaningful property naming conventions, such as using dot-separated hierarchical structures, to improve configuration readability and maintainability.

Spring Boot Externalized Configuration Mechanism

Spring Boot supports multiple configuration sources through sophisticated property source loading mechanisms. Property file loading follows a clear priority order, ensuring environment-specific configurations can correctly override default values.

Configuration data file search paths include:

This hierarchical search strategy enables applications to maintain consistent configuration management across different environments while supporting environment-specific configuration overrides.

Type-Safe Configuration Properties Comparison

While the @Value annotation provides flexible property injection, for complex configuration structures, Spring Boot recommends using @ConfigurationProperties for type-safe configuration binding.

Comparative analysis of the two approaches:

<table><tr><th>Feature</th><th>@ConfigurationProperties</th><th>@Value</th></tr><tr><td>Relaxed Binding Support</td><td>Yes</td><td>Limited</td></tr><tr><td>Metadata Support</td><td>Yes</td><td>No</td></tr><tr><td>SpEL Expressions</td><td>No</td><td>Yes</td></tr><tr><td>Validation Support</td><td>Yes</td><td>No</td></tr>

For simple list configurations, Spring EL combined with @Value provides the most direct solution; for complex configuration objects, @ConfigurationProperties offers better type safety and validation support.

Advanced Application Scenarios

In actual enterprise-level applications, list configurations often need to handle more complex requirements. Spring EL supports various collection operations, including advanced functions such as filtering and mapping.

For example, if empty values need to be filtered, you can use:

@Value("#{'${my.list.of.strings}'.split(',').?[!#this.empty]}")
private List<String> filteredList;

This expression, after splitting the string, uses .?[!#this.empty] to filter out empty string elements, ensuring the collection contains only valid data.

Performance Considerations and Best Practices

Spring EL expressions are parsed and cached during application startup, with minimal runtime performance overhead. However, overly complex expressions may affect startup time, so it's recommended to keep expressions concise.

Best practice recommendations:

Error Handling and Debugging

Various exceptional situations may occur during configuration loading. Common errors include properties not found, format mismatches, etc. Spring provides comprehensive error information to help developers quickly locate issues.

Debugging techniques:

Conclusion

Through Spring EL expressions combined with the @Value annotation, developers can achieve elegant mapping from property files to Java collections in a declarative manner. This approach not only avoids tedious custom code but also maintains configuration simplicity and maintainability. Combined with Spring Boot's powerful externalized configuration mechanisms, it provides comprehensive configuration management solutions for modern Java applications.

In actual project development, it's recommended to choose appropriate configuration methods based on specific requirements: use Spring EL expressions for simple list configurations, and adopt type-safe @ConfigurationProperties for complex configuration objects. This layered strategy ensures development efficiency while providing sufficient flexibility and robustness.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.