21

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.

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

30

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

dunni
  • 43,386
  • 10
  • 104
  • 99
1

You have multiple options:

# in case of set it hardcoded ALWAYS true
pdk.populatedemo=true

# in case of set it hardcoded ALWAYS false
pdk.populatedemo=false

# in case of set it dynamically,
# where isPopulatedemo is a system property or an environment variable having a string value either "true" or "false"
pdk.populatedemo=${isPopulatedemo} 
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88