【发布时间】:2016-07-01 13:05:20
【问题描述】:
我的 Spring Boot 应用程序中有一个 typesafe configuration 的课程:
@Component
@ConfigurationProperties(prefix = "props")
@Getter
@Setter
public class Properties {
private String param1 = "val1";
private String param2 = "val2";
}
稍后我尝试在带有注释的 bean 中的字段上使用它:@Value("${props.param1}")
但我在应用程序启动时遇到以下异常,直到我在 application.properties 中为我的自定义属性指定一个值
原因:java.lang.IllegalArgumentException:无法解析 字符串值“${props.param1}”中的占位符“props.param1”
如何使 Spring Boot 应用程序使用默认值而不在 application.properties 中指定值?
当我在application.properties 中键入属性并且生成的spring-configuration-metadata.json 文件中有一个defaultValue 时,我在IDE 中看到了默认值。我想这个默认值应该与 spring 一起使用,直到我在我的属性文件中覆盖它,但由于未知原因,我从上面得到了异常。
【问题讨论】:
-
您究竟是如何使用
Properties组件的?使用@Value或@Autowired? -
@AliDehghani 我提到了,我使用了@Value)
-
@AliDehghani 我尝试设置它而不是在我的主类上设置 - 没有区别
标签: java spring spring-boot