6

I'm new to Springboot. This is the problem I'm trying to solve. I've an application.yml file with the following property:

kinesis:
    streaming:
        client:
            featuretoggle:
                kinesisSenderFeature: true

I tried to access the value of the KinesisSenderFeature using the code:

@Value("${kinesis.streaming.client.featuretoggle.kinesisSenderFeature}")
private boolean featureToggle;

as well as

@Value("${kinesis.streaming.client.featuretoggle.kinesisSenderFeature}")
private Boolean featureToggle;

The PropertySourcesPlaceholderConfigurer bean is defined as:

 @Bean
    @Primary
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("application.yml"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    }

When I try to build, ApplicaitonContext fails to load with the following error:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rabbitMessageConsumer': Unsatisfied dependency expressed through field 'featureToggle'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Boolean'; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [${kinesis.streaming.client.featuretoggle.kinesisSenderFeature}]

What I find weird is, it is trying to convert the string: [${kinesis.streaming.client.featuretoggle.kinesisSenderFeature}] into a boolean value, and I believe- is not reading the value of the property from the yaml file.

Yes, I did see:

I don't want to create a bean around this property as this is just a boolean flag.

Note: If i put a :default in the @Value, the build succeeds- but I believe that is only because the read from yaml failed, and defaulted with the value I gave.

@Value("${kinesis.streaming.client.featuretoggle.kinesisSenderFeature:false}")
private boolean featureToggle;

Note: As pointed out by @Andreas in the comment, I tried giving

//In application.yml
kinesisSenderFeature:false

//In code
@Value("${kinesisSenderFeature}")
private boolean featureToggle;

Even that didn't work. But there are other properties that are read from the yaml without any problem. It is the standard application.yml that is in src/main/resources which I believe should be read by default?

Any help will be greatly appreciated. Thanks!

Community
  • 1
  • 1
Jeevs
  • 699
  • 2
  • 10
  • 19
  • 1
    For the sake of *researching* the problem, did you try defining a simple `abc: true` property and use that? You know, to see if any property is being read from that file. – Andreas May 11 '17 at 15:43
  • Thank you for pointing that out. No, even that didn't work. I'll edit the question accordingly. – Jeevs May 11 '17 at 15:53
  • 1
    Did you try without PropertySourcesPlaceholderConfigurer bean. I mean remove this bean and see if the default PropertyReader from Spring picks it up properly – pvpkiran May 11 '17 at 18:28

1 Answers1

3

As @pvpkiran pointed out, you do not need the PropertySourcesPlaceholderConfigurer. Just put the application.yml on the classpath, Spring Boot picks it up and assigns the Booleanvalue. Works like a charm, just tested it with Spring Boot 1.5.2.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66