Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a List from properties file and load with spring annotation @Value

I want to have a list of values in a .properties file, ie:

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

And to load it in my class directly, ie:

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

As I understand, an alternative of doing this is to have it in the spring config file, and load it as a bean reference (correct me if I'm wrong), ie

<bean name="list">  <list>   <value>ABC</value>   <value>CDE</value>   <value>EFG</value>  </list> </bean> 

But is there any way of doing this? using a .properties file? ps: I would like to do this with out any custom code if possible.

like image 932
JackDev Avatar asked Sep 25 '12 04:09

JackDev


People also ask

How read multiple values from properties in spring boot?

We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application.

What is the use of @value annotation in spring?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.

What does the @value annotation do?

@Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument. It is commonly used for injecting values into configuration variables - which we will show and explain in the next part of the article.


2 Answers

Using Spring EL:

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

Assuming your properties file is loaded correctly with the following:

my.list.of.strings=ABC,CDE,EFG 
like image 107
Wilhelm Kleu Avatar answered Oct 15 '22 09:10

Wilhelm Kleu


Since Spring 3.0, you can add a line like

<bean id="conversionService"      class="org.springframework.context.support.ConversionServiceFactoryBean" /> 

to your applicationContext.xml (or where you configure things). As Dmitry Chornyi points out in a comment, Java based configuration looks like:

@Bean public ConversionService conversionService() {     return new DefaultConversionService(); } 

This activates the new configuration service which supports converting String to Collection types. If you do not activate this configuration service, Spring falls back on its legacy property editors as configuration services, which do not support this kind of conversion.

Converting to collections of other types works, too:

@Value("${my.list.of.ints}") private List<Integer> myList 

will work with a line like

 my.list.of.ints= 1, 2, 3, 4 

No problems with whitespace there, the ConversionServiceFactoryBean takes care of it.

See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#core-convert-Spring-config

In a Spring application, you typically configure a ConversionService instance per Spring container (or ApplicationContext). That ConversionService will be picked up by Spring and then used whenever a type conversion needs to be performed by the framework. [...] If no ConversionService is registered with Spring, the original PropertyEditor-based system is used.

like image 44
Clemens Klein-Robbenhaar Avatar answered Oct 15 '22 10:10

Clemens Klein-Robbenhaar