Springboot yaml config does not read boolean

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

kinesis:
    streaming:
        client:
            featuretoggle:
                kinesisSenderFeature: true

I tried to access the KinesisSenderFeature value using code:

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

and

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

The PropertySourcesPlaceholderConfigurer bean property 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 does not 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 strange is trying to convert the string: [$ {kinesis.streaming.client.featuretoggle.kinesisSenderFeature}] to a boolean, and I believe that it does not read the value of the property from the yaml file.

Yes, I saw:

bean , .

. a: default @Value, , , , yaml , .

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

. @Andreas ,

//In application.yml
kinesisSenderFeature:false

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

. , yaml - . application.yml, src/main/resources, , , ?

. !

+4
1

@pvpkiran, PropertySourcesPlaceholderConfigurer. application.yml , Spring Boolean. , Spring Boot 1.5.2.

+2

Source: https://habr.com/ru/post/1676938/


All Articles