4

This may be a stupid questions, but is it possible to populate a list form an application.properties file in Spring Boot. Here is a simple example:

public class SomeClass {
    @Value("${hermes.api.excluded.jwt}")
    private List<String> excludePatterns = new ArrayList<>();
    // getters/settings ....
}

application.properties

// Is something along these lines possible????
hermes.api.excluded.jwt[0]=/api/auth/
hermes.api.excluded.jwt[1]=/api/ss/

I know I could explode a comma separated string, but I was just curious if there is a native spring boot way to do this?

csyperski
  • 992
  • 3
  • 15
  • 33
  • Have you tried? It seems that trying this out takes less time than writing a question :) – Nikem May 31 '16 at 18:04
  • Yes, if it possible I don't know the secret syntax in the properties file, my question just used what I thought was obvious, I get....Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.cwssoft.reportout.filter.JwtFilter.excludePatterns; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'hermes.api.excluded.jwt' in string value "${hermes.api.excluded.jwt}" – csyperski May 31 '16 at 18:06
  • 3
    I guess it's http://stackoverflow.com/questions/12576156/reading-a-list-from-properties-file-and-load-with-spring-annotation-value – zapl May 31 '16 at 18:10
  • That's surprising, I would have thought there was a cleaner way (vs a simple string.split) since it is common in an applicationContext file. Thanks – csyperski May 31 '16 at 18:19
  • 1
    Oh, https://github.com/spring-projects/spring-boot/issues/501#issuecomment-41066826 you don't have to split, it's apparently enabled by default in boot nowadays. + YAML seems to support your syntax: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading-yaml – zapl May 31 '16 at 18:22
  • My issue is I can't use Yaml due to: "24.6.4 YAML shortcomings YAML files can’t be loaded via the @PropertySource annotation. So in the case that you need to load values that way, you need to use a properties file." But it turns out you can populate an array using a properties file. So for me this works: private String[] excludePatterns with a comma separated list. Source: http://stackoverflow.com/questions/6212898/spring-properties-file-get-element-as-an-array – csyperski May 31 '16 at 18:41

1 Answers1

11

Turns out it does work. However, it seems you have to use configuration properties, since simple @Value("${prop}") seems to use a different path under the hood. (There are some hints to DataBinder in this secion. Not sure if related.)

application.properties

foo.bar[0]="a"
foo.bar[1]="b"
foo.bar[2]="c"
foo.bar[3]="d"

and in code

@Component
@ConfigurationProperties(prefix="foo")
public static class Config {
    private final List<String> bar = new ArrayList<String>();
    public List<String> getBar() {
        return bar;
    }
}

@Component
public static class Test1 {
    @Autowired public Test1(Config config) {
        System.out.println("######## @ConfigProps " + config.bar);
    }
}

results in

######## @ConfigProps ["a", "b", "c", "d"]

While

@Component
public static class Test2 {
    @Autowired public Test2(@Value("${foo.bar}") List<String> bar) {
        System.out.println("######## @Value " + bar);
    }
}

results in

java.lang.IllegalArgumentException: Could not resolve placeholder 'foo.bar' in string value "${foo.bar}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(...
    ...
zapl
  • 63,179
  • 10
  • 123
  • 154