Home >Java >javaTutorial >How to Read a List from a Properties File Using Spring's @Value Annotation?
In a Spring application, you can easily read a list of values from a .properties file using the @Value annotation. This allows you to define a list field in your Java class and automatically populate it with values from the properties file at runtime.
To achieve this, you can use the following syntax:
@Value("#{'${my.list.of.strings}'.split(',')}") private List<String> myList;
Here, we use Spring Expression Language (SpEL) to split the string value of the "my.list.of.strings" property (expected format: "ABC,CDE,EFG") into a list of strings. You can assume that your properties file is properly loaded and the "my.list.of.strings" property is defined with the desired values.
This approach avoids the need to manually parse the list in your custom code or using a bean definition in the Spring configuration file.
The above is the detailed content of How to Read a List from a Properties File Using Spring's @Value Annotation?. For more information, please follow other related articles on the PHP Chinese website!