Spring application.properties 文件中的布尔值?

boolean values in Spring application.properties file?

是否可以在 Spring 配置文件中使用布尔值?

我在我的 bean 中写了以下字段:

@Value("${pdk.populatedemo}")
private boolean populateDemo;

但如果导致以下异常:

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;]

这里我试过了

pdk.populatedemo=1;

application.properties 中。我也尝试了 =true 和其他一些。

布尔类型的正确值为

pdk.populatedemo=true

1 不是布尔字段的有效值,您不得在 属性 文件中使用分号作为布尔值(正如您在错误消息中清楚看到的那样)。

您有多种选择:

# 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}