Java – boolean values in Spring application.properties file

javaproperties-filespring

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.

Best Answer

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).