javaspringspring-annotations

Spring @Value empty list as default


Is there a way to set an empty list as default value for a property in Spring, something like:

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

Obviously not new ArrayList, but I need an empty list there instead.


Solution

  • 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;