Reading a List from properties file and load with Spring annotation @Value
Asked Answered
F

17

325

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

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

And to load it in my class directly, i.e.:

@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), i.e.

<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?

P.S.

I would like to do this without any custom code if possible.

Fibroin answered 25/9, 2012 at 4:19 Comment(1)
how to read List<int[]> range using @valueUsurious
M
599

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
Morganatic answered 25/9, 2012 at 9:41 Comment(16)
Gave me a "Caused by: javax.el.ELException: Failed to parse the expression [#{'${my.list.of.strings}'.split(',')}"] Did it work for you? Maybe I did something incorrectly.Fibroin
Works perfectly when I try it. What Spring version are you using?Morganatic
I checked it again using the same version you are using. Copied the Spring EL exactly as in the post and it works. What is different though is if I make an error in my EL I get a org.springframework.expression.spel.SpelEvaluationException exception and not javax.el.ELException. Is your objected created by Spring?Morganatic
Works perfectly with Spring 3.2.Latecomer
How about empty property my.list.of.strings=? I would expect such functionality retruning empty list where here it will be one item (empty string), wouldn't it?Stillbirth
I created custom static method to do that. First checking whether the input String is null or empty, in that case return empty list. Otherwise do the split. You can call static methods in EL easily.Stillbirth
Also note that the suggested solution doesn't do trimming so values like item1, item2, item3 may give you result you don't really expect (hint: spaces).Stillbirth
If you need to trim empty spaces use regex for split argument.. something like @Value("#{'${my.list.of.strings}'.split(',\\s*')}") Terranceterrane
Be careful not to put space before #{ e.g. @Value(" #{'${my.list.of.strings}'.split(',')}") or it won't split property just provide list with one non splitted element.Toneytong
Does it support in XML?Aphoristic
How can one read similar property and create a list of String in applicationContext.xml?Joli
This does not work with @CrossOrigin(origins = {"${allowed.cors.urls}"}) Anyone has solution for annotation level?Eckhart
Can set be used here in place of list?Curable
when I try to keep a default value for empty list by adding ': '. there's still one element in the list with value of null when i print the list. how to resolve this. @Value("#{'${list.whitelist: }'.split(',')}") List<UUID> WhiteListCompromise
my.list.of.strings={'ABC','CDE','EFG'} If you use this syntax, then you don't need to use custom expression to split.Endymion
Outdated answer. Since spring boot 2 the string will automatically be split for you.Aigneis
L
113

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.

Leclaire answered 30/4, 2015 at 14:23 Comment(3)
Java configuration: @Bean public ConversionService conversionService() { return new DefaultConversionService(); }Misshape
Outside avoiding to repeat yourself with split() in every expression, it also properly handles an empty list instead of giving you [null]Decasyllabic
It is not working if the property is overrided (Exists in multiple property sources.)Morales
C
61

If you are reading this and you are using Spring Boot, you have 1 more option for this feature

Usually comma separated list are very clumsy for real world use case (And sometime not even feasible, if you want to use commas in your config):

[email protected],[email protected],[email protected],.....

With Spring Boot, you can write it like these (Index start at 0):

email.sendTo[0][email protected]
email.sendTo[1][email protected]
email.sendTo[2][email protected]

And use it like these:

@Component
@ConfigurationProperties("email")
public class EmailProperties {

    private List<String> sendTo;

    public List<String> getSendTo() {
        return sendTo;
    }

    public void setSendTo(List<String> sendTo) {
        this.sendTo = sendTo;
    }

}


@Component
public class EmailModel {

  @Autowired
  private EmailProperties emailProperties;

  //Use the sendTo List by 
  //emailProperties.getSendTo()

}



@Configuration
public class YourConfiguration {
    @Bean
  public EmailProperties emailProperties(){
        return new EmailProperties();
  }

}


#Put this in src/main/resource/META-INF/spring.factories
  org.springframework.boot.autoconfigure.EnableAutoConfiguration=example.compackage.YourConfiguration
Caryncaryo answered 1/12, 2017 at 3:23 Comment(1)
I have a problem where the other solutions described here do not work due to escaped commas not beeing recognized. Is there a way to do this solution with Spring 4?Lightish
L
58

If you are using Spring Boot 2, it works as is, without any additional configuration.

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

@Value("${my.list.of.strings}")
private List<String> myList;
Lightness answered 5/12, 2018 at 18:53 Comment(3)
didn't work for me, I have to use the Spring EL as shown above.Colchis
or even private List<String> myList;Apostatize
Yes it worked for me as well: I am using Spring Boot 2.2.6Compote
D
49

By specifying the the my.list.of.strings=ABC,CDE,EFG in .properties file and using

@Value("${my.list.of.strings}") private String[] myString;

You can get the arrays of strings. And using CollectionUtils.addAll(myList, myString), you can get the list of strings.

Drafty answered 25/9, 2012 at 4:57 Comment(1)
I get only the first String "ABC"Baerman
N
20

Have you considered @Autowireding the constructor or a setter and String.split()ing in the body?

class MyClass {
    private List<String> myList;

    @Autowired
    public MyClass(@Value("${my.list.of.strings}") final String strs) {
        myList = Arrays.asList(strs.split(","));
    }

    //or

    @Autowired
    public void setMyList(@Value("${my.list.of.strings}") final String strs) {
        myList = Arrays.asList(strs.split(","));
    }
}

I tend to prefer doing my autowiring in one of these ways to enhance the testability of my code.

Nanci answered 25/9, 2012 at 4:42 Comment(0)
H
14

If you are using latest Spring framework version(Spring 3.1+ I believe), you don't need to those string split stuff in SpringEL,

Simply add PropertySourcesPlaceholderConfigurer and DefaultConversionService in your Spring's Configuration class ( the one with annotated with Configuration ) e.g,

@Configuration
public class AppConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

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

and in your class

@Value("${list}")
private List<String> list;

and in the properties file

list=A,B,C,D,E

Without DefaultConversionService, you can only take comma separated String into String array when you inject the value into your field, but DefaultConversionService does a few convenient magic for you and will add those into Collection, Array, etc. ( check the implementation if you'd like to know more about it )

With these two, it even handles all the redundant whitespaces including newline, so you don't need to add additional logics to trim them.

Hinkel answered 16/11, 2018 at 4:8 Comment(1)
The adding of this configuration is working but something strange happens: I cannot use @Value for File type, I must change File with Resource.Taut
C
12

I am using Spring Boot 2.2.6

My property file:

usa.big.banks= JP Morgan, Wells Fargo, Citigroup, Morgan Stanley, Goldman Sachs

My code:

@Value("${usa.big.banks}")
    private List<String> bigBanks;

@RequestMapping("/bigbanks")
    public String getBanks() {
        System.out.println("bigBanks = " + bigBanks);
        return bigBanks.toString();
    }

It works fine

Compote answered 20/4, 2020 at 6:31 Comment(0)
R
10

All the above answers are correct. But you can achieve this in just one line. Please try following declaration and you will get all the comma separated values in a String list.

private @Value("#{T(java.util.Arrays).asList(projectProperties['my.list.of.strings'])}") List<String> myList;

And also you need to have the following line defined in your xml configuration.

<util:properties id="projectProperties" location="/project.properties"/>

just replace the path and file name of your properties file. And you are good to go. :)

Hope this helps you. Cheers.

Rehearing answered 25/9, 2012 at 6:57 Comment(1)
This worked for me, but I had to phrase the annotation like this: @Value("#{T(java.util.Arrays).asList('${my.list.of.strings}')}")Makalu
H
6

My preferred way (for strings, in particular), is the following one:

admin.user={'Doe, John','Headroom, Max','Mouse, Micky'}

and use

@Value("#{${admin.user}}")
private List<String> userList;

In this way, you can include also commas in your parameter. It works also for Sets.

Hawkweed answered 29/1, 2020 at 15:6 Comment(2)
What/where is the underlying code that does this conversion for Spring?Ruiz
It's SpELl that reads property admin.user ( the '${admin.user}', see docs.spring.io/spring-framework/docs/current/reference/html/…) and then parses what is inside ( the '#{....}' syntax, see docs.spring.io/spring-framework/docs/current/reference/html/…).Hawkweed
L
4

you can do this with annotations like this

 @Value("#{T(java.util.Arrays).asList('${my.list.of.strings:a,b,c}')}") 
    private List<String> mylist;

here my.list.of.strings will be picked from the properties file, if its not there, then the defaults a,b,c will be used

and in your properties file, you can have something like this

my.list.of.strings=d,e,f

Leverrier answered 9/9, 2013 at 20:58 Comment(0)
P
3

Beware of spaces in the values. I could be wrong, but I think spaces in the comma-separated list are not truncated using @Value and Spel. The list

foobar=a, b, c

would be read in as a list of strings

"a", " b", " c"

In most cases you would probably not want the spaces!

The expression

@Value("#{'${foobar}'.trim().replaceAll(\"\\s*(?=,)|(?<=,)\\s*\", \"\").split(',')}")
private List<String> foobarList;

would give you a list of strings:

"a", "b", "c".

The regular expression removes all spaces just before and just after a comma. Spaces inside of the values are not removed. So

foobar = AA, B B, CCC

should result in values

"AA", "B B", "CCC".
Persistence answered 4/11, 2013 at 10:22 Comment(1)
The split() method uses internally regex as delimiter, so your example can be simplified: <br/> @Value("#{'${foobar}'.trim().split( *, *)}")Ananthous
P
3

In my case of a list of integers works this:

@Value("#{${my.list.of.integers}}")
private List<Integer> listOfIntegers;

Property file:

my.list.of.integers={100,200,300,400,999}
Prudish answered 1/10, 2019 at 9:35 Comment(1)
or @Value("#{${my.set.of.integers}}") private Set<Integer> setOfIntegers;Gayl
L
2

I think this is simpler for grabbing the array and stripping spaces:

@Value("#{'${my.array}'.replace(' ', '').split(',')}")
private List<String> array;
Lexicologist answered 2/10, 2018 at 13:40 Comment(0)
B
1

Consider using Commons Configuration. It have built in feature to break an entry in properties file to array/list. Combing with SpEL and @Value should give what you want


As requested, here is what you need (Haven't really tried the code, may got some typoes, please bear with me):

In Apache Commons Configuration, there is PropertiesConfiguration. It supports the feature of converting delimited string to array/list.

For example, if you have a properties file

#Foo.properties
foo=bar1, bar2, bar3

With the below code:

PropertiesConfiguration config = new PropertiesConfiguration("Foo.properties");
String[] values = config.getStringArray("foo");

will give you a string array of ["bar1", "bar2", "bar3"]

To use with Spring, have this in your app context xml:

<bean id="fooConfig" class="org.apache.commons.configuration.PropertiesConfiguration">
    <constructor-arg type="java.lang.String" value="classpath:/Foo.properties"/>
</bean>

and have this in your spring bean:

public class SomeBean {

    @Value("fooConfig.getStringArray('foo')")
    private String[] fooArray;
}

I believe this should work :P

Bloomfield answered 1/10, 2012 at 5:39 Comment(1)
could you be more specific about the method and class to use, and actual sample code sniplet would be great.Fibroin
D
1

The Answer

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

works as expected for the comma separated values. To handle null (when property not specified) added the default value(': 'after the property name) as empty string as below:

@Value("#{'${my.list.of.strings: }'.split(',')}")
Dead answered 12/5, 2021 at 17:26 Comment(0)
L
0

if using property placeholders then ser1702544 example would become

@Value("#{myConfigProperties['myproperty'].trim().replaceAll(\"\\s*(?=,)|(?<=,)\\s*\", \"\").split(',')}") 

With placeholder xml:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
    <property name="properties" ref="myConfigProperties" />
    <property name="placeholderPrefix"><value>$myConfigProperties{</value></property>
</bean>    

<bean id="myConfigProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
     <property name="locations">
         <list>
                <value>classpath:myprops.properties</value>
         </list>
     </property>
</bean> 
Labelle answered 6/12, 2013 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.