1

I need to load a java.util.List<Integer> from a properties file that provides the comma separated integers.

ids = 1230, 34, 2587, 31

So far I've tried a identifiers.properties file with the above contents and loading it in a bean with a spring xml file:

    <bean id="identifiersList" class="MyIdHolderClass">
        <property name="idsList" value="${ids}"/>
    </bean>

But I get an error because the java.util.List<Integer> ids cannot store a String, which is what Spring is trying to put in it.

Is there a way to spring-inject a list of numbers from a properties file?

Just for context: what I am trying to achieve is to load a Map<Integer, Float> from a properties file. But failing to load it, I decided to split it in loading the keys and loading the values. (It will end up as a map that tells the price tag for each product identifier).

manuelvigarcia
  • 1,696
  • 1
  • 22
  • 32
  • Why a `.properties` file and not something more suited to hold array/list data like json or xml? – Thomas Feb 21 '17 at 17:04
  • @Thomas It also crossed my mind, but the properties format is more easily achieved exporting the data from external sources. To get the .xml working it would need heavy editing to add all tags in place. Unless you have some nice suggestion... – manuelvigarcia Feb 21 '17 at 22:25
  • It is indeed solved in the suggested question. – manuelvigarcia Feb 22 '17 at 13:39

2 Answers2

2

Try adding this bean to your configuration:

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

It will add support to convert String to Collection:

Note that DefaultConversionService registers converters automatically which are appropriate for most environments. This includes collection converters, scalar converters, and also basic Object to String converters. The same converters can be registered with any ConverterRegistry using the static addDefaultConverters method on the DefaultConversionService class.

Reference: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

alturkovic
  • 990
  • 8
  • 31
  • This answer --more elaborated in the [suggested question](http://stackoverflow.com/a/29970335/5108777)-- is the one I ended up with. Very convenient being relieved from splitting the string and converting tokens into `Float`. – manuelvigarcia Feb 22 '17 at 13:43
  • While true the post by Maciej is more accurate today as this is more easily done with spel expressions. – Mr Chow Aug 14 '20 at 19:35
  • 1
    @MrChow this is not more easily done with SpEL. Exposing the bean once in a configuration file is much easier than adding `.split(',')}` everywhere a list is wired up. Also, if the coder forgets to add `.split(',')}` the list will still be populated so the omission may not be immediately apparent. – Paul Nov 24 '21 at 13:44
  • @Paul thats fair – Mr Chow Nov 26 '21 at 15:33
1

I think you should inject the properties like this:

<bean id="identifiersList" class="MyIdHolderClass">
   <property name="idsList" value="#{'${ids}'.split(',')}"/>
</bean>
manuelvigarcia
  • 1,696
  • 1
  • 22
  • 32
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • If you expose `DefaultConversionService` as a bean you won't need to `split` in SpEL. – Paul Nov 24 '21 at 13:39