Spring @Value empty list as default Spring @Value empty list as default spring spring

Spring @Value empty list as default


After taking a look at SpEL specification and combined with @javaguy's answer I came up with this:

@Value("${my.list.of.strings:}#{T(java.util.Collections).emptyList()}")private List<String> myList;


Actually, in the current Spring versions works just one symbol : with an empty defaultvalue.

The full example that I'm using:

@Value("${my.custom.prop.array:}")private List<Integer> myList;

To be sure and safer I also adding init to the List variable:

@Value("${my.custom.prop.array:}")private List<Integer> myList = new ArrayList<>();


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

works for me, using Spring 5.0.x (gives empty list, if your my.list.of.strings property is not configured in context)

afterwards you can easily do something like

CollectionUtils.isNotEmpty(myList)