Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot yaml config not reading boolean

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:

  • Mapping list in Yaml to list of objects in Spring Boot
  • Spring boot YAML Config not reading all values
  • Evaluating spring @value annotation as primitive boolean

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!

like image 245
Jeevs Avatar asked Oct 18 '22 12:10

Jeevs


1 Answers

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.

like image 160
P.J.Meisch Avatar answered Nov 04 '22 00:11

P.J.Meisch