javaspringannotations

Spring @Value default value for String[]


I'm wondering how I can set a default Value for String[].

@Value("${blacklist}")
private String[] blacklist;

In runtime the list is filled via my application.yml. In Testcases it is null and I will not set any value for each Testcase. Furthermore if there are no entries in die application.yml it have to be an empty array.

Usage:

for (String varToRemove : blacklist) {
        vars.remove(varToRemove );
 }

Pls no answers like "Why you do not just null-check it?". I want to know how to set the default value :)


Solution

  • I was so concentrated on the Annotation that I forgot the simplest way (and it works):

    @Value("${blacklist:}")
    private String[] blacklist= new String[0];