Is it possible to have boolean values in Spring configuration file?
I wrote the following field in my bean:
@Value("${pdk.populatedemo}")
private boolean populateDemo;
but if causes the following exception:
Could not autowire field: private boolean com.inthemoon.pdk.data.DatabaseService.populateDemo; nested exception is org.springframework.beans.TypeMismatchException:
Failed to convert value of type [java.lang.String] to required type [boolean]; nested exception is java.lang.IllegalArgumentException:
Invalid boolean value [1;]
here I tried
pdk.populatedemo=1;
in application.properties
. I also tried =true
and some others.
When the Properties of your file are loaded you can use the Boolean -Class to get the Properties: Boolean. getBoolean("your. property");
To represent a string in YAML , we use a single quote ( ' ) or a double quote ( " ) and the actual string value within these quotes. We use the standard numeric literals for integers and floating-point numbers. Boolean values can be represented in YAML using the keywords like true and false or Yes and No .
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.
Java boolean operators are denoted by |, ||, &, &&, <, >, <=, >=, ^, != , ==. These logical boolean operators help in specifying the condition that will have the two return values – “true” or “false”.
The correct value for a boolean type would be
pdk.populatedemo=true
1
is not a valid value for a boolean field and you must not use semicolons in your property file for a boolean value (as you clearly can see in the error message).
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