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!
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 Boolean
value. Works like a charm, just tested it with Spring Boot 1.5.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With