17

I have the following in my application.properties file

some.server.url[0]=http://url
some.server.url[1]=http://otherUrl

How do I refer to the array of properties using the @Value anotation inside a @Bean method?

I am using Java 6 with Tomcat 7 and Spring boot 1.4

SebS
  • 571
  • 3
  • 6
  • 16

3 Answers3

28

I was also having the same problem as you mentioned and it seems using index form on application.properties was not working for me either.

To solve the problem I did something like below

some.server.url = url1, url2

Then to get the those properties I simply use @Value

@Value("${some.server.url}")
private String[] urls ;

Spring automatically splits the String with comma and return you an Array. AFAIK this was introduced in Spring 4+

If you don't want comma (,) as seperator you have to use SpEL like below.

@Value("#{'${some.server.url}'.split(',')}")
private List<String> urls;

where split() accepts the seperator

A0__oN
  • 8,740
  • 6
  • 40
  • 61
8

You can use a collection.

@Value("${some.server.url}")
private List<String> urls;

You can also use a configuration class and inject the bean into your other class:

@Component
@ConfigurationProperties("some.server")
public class SomeConfiguration {
    private List<String> url;

    public List<String> getUrl() {
        return url;
    }

    public void setUrl(List<String> url) {
        this.url = url;
    }
}
Shawn Clark
  • 3,330
  • 2
  • 18
  • 30
  • `@Bean public List someUriList(@Value("${some.server.url}") List urlList)` returns `java.lang.IllegalArgumentException: Could not resolve placeholder 'some.server.url' in string value "${some.server.url}"` – SebS Aug 17 '16 at 00:16
  • Based on your question you had the property as `some.url` not `some.server.url`. Is that the issue? – Shawn Clark Aug 17 '16 at 00:17
  • No. I have updated the initial example to reflect what is my application.properties – SebS Aug 17 '16 at 00:25
  • Not sure why that isn't working for you but I have also included another way to do it and you can see if that works better for you. – Shawn Clark Aug 17 '16 at 00:57
  • Using Java 6 with Tomcat 7 and Spring boot 1.4. Has that got anything to do with it? – SebS Aug 17 '16 at 01:32
  • In your example `@ConfigurationProperties("some.server")` I have other properties under some.server that do not belong in the url array. – SebS Aug 17 '16 at 01:39
  • That's fine... They just won't be used in the context of that bean. If you want them assigned as well then include properties for them and they will be populated. – Shawn Clark Aug 17 '16 at 01:54
7

Follow these steps

1) @Value("${some.server.url}") private List urls;

2) @ConfigurationProperties("some.server") public class SomeConfiguration {

3) You should have getter and setter for instance variable 'urls'

user3567935
  • 134
  • 1
  • 2
  • I found that the accepted answer (2) was not correct: 1. The java name has to match **exactly** the name in the properties file, i.e. inputQueues[0] and so on in properties file needs a variable name inputQueues. Very important: you have to create an instance of the list like List inputQueues = new ArrayList<>(); 2. You don't need a prefix 3. You need the annotation @ConfigurationProperties 4. You need the setters/getters 5. You don't need the $Value annotation. – Guenther Mahr Feb 14 '19 at 09:50