【问题标题】:boolean values in Spring application.properties file?Spring application.properties 文件中的布尔值?
【发布时间】:2016-04-04 09:50:09
【问题描述】:

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 和其他一些。

【问题讨论】:

  • in application.properties 我也试过=true 末尾有分号 ?

标签: java spring properties-file


【解决方案1】:

布尔类型的正确值是

pdk.populatedemo=true

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

【讨论】:

  • 在springboot 2/spring 5 "pdk.populatedemo=true" 为false,机制改为1/0
【解决方案2】:

您有多种选择:

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

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2013-02-02
    • 2017-08-28
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2019-05-20
    • 2015-08-12
    相关资源
    最近更新 更多