2

After reading a couple of answers on StackOverflow, I tried to implement it as recommended, but I am still not getting a string list in my @Component.

Here is my application.yml file

appName:
    db:
        host: localhost
        username: userX
        password: passX
        driverClassName: org.postgresql.Driver
    shipment:
        providers:
            supported:
                - one
                - two

And my @Component class is

@Component
@ConfigurationProperties(prefix = "appName.shipment.providers")
public class ProviderUtil {

    List<String> supported;

    /* This class has other util methods as well */
}

Here, I expect that supported would have a list of strings one and two but is null. Can someone help me understand what is happening here and how to resolve it?

Prectron
  • 282
  • 2
  • 11
Em Ae
  • 8,167
  • 27
  • 95
  • 162

2 Answers2

1

Do you have spring-boot-configuration-processor dependency on your classpath? Try to look here

And also change your bean from @Component to @Configuration

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
bilak
  • 4,526
  • 3
  • 35
  • 75
  • Yup, i have added it exactly the way it is mentioned in the docs. with `optional` value set to `true` and I used `component` because of this reponse >> http://stackoverflow.com/questions/26699385/spring-boot-yaml-configuration-for-a-list-of-strings ... That being said, I have tried both with `configuration` and `component` but no luck – Em Ae Jul 27 '16 at 17:08
  • Try to share your code to see if we can help you somehow. – bilak Jul 27 '16 at 17:15
  • I figured out the issue. I hadn't provided the getters/setters. I had made the field public and i thought it would work – Em Ae Jul 27 '16 at 19:09
0

I followed this post and added the getters and setters for the list. It worked out for me.

Code:

@Service("testservice")
@ConfigurationProperties(prefix="es")
public class TestService {

    @Value("${url.endPoint}")
    private String endpointUrl;

    private List<String> scope;

    public List<String> getScope() {
        return scope;
    }

    public void setScope(List<String> scope) {
        this.scope = scope;
    }

    // ...
}
Prectron
  • 282
  • 2
  • 11
SPS
  • 31
  • 3