boolean values in Spring application.properties file?

☆樱花仙子☆ 提交于 2020-02-23 09:03:52

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/34529216/boolean-values-in-spring-application-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!