Home > Enterprise >  Spring, inject list value with SPEL using split and default empty
Spring, inject list value with SPEL using split and default empty

Time:07-20

I want this property mapped on application.properties

my.list=a;b;c

converted to a List (or eventually a String[]); there are 2 requirements;

  1. my.list is separated by ; char
  2. my.list could be empty

I tried with:

@Value("#{T(java.util.Arrays).asList('${my.list:}'.split(';'))}")

but it doesn't work as expected, as passing multiple values convert the list with a single element "a,b,c".

PS I already know there is a very similare SO question, however the solution was not tested with separator different from ','.

CodePudding user response:

Just use

@Value("#{'${my.list:}'.split(';')}#{T(java.util.Collections).emptyList()}")
List<String> listValue;
  • Related