【发布时间】:2019-11-27 15:34:51
【问题描述】:
我有一个带有 gradle 的 SpringBoot 2.1.7.RELEASE 项目。当我尝试使用 @ConfigurationProperties 时出现错误
我尝试绑定的属性存在于我的 application-default.properties 中,如果我使用 Itellij 运行项目,我可以看到该属性已被提取到我的组件中。
如果我启用 @EnableConfigurationProperties 会出现错误。
我的应用程序-default.properties
app.forwarding-endpoint=localhost:8080
我的 AppProperties.java
@ConfigurationProperties(prefix = "app", ignoreUnknownFields = false)
@Validated
@Data
public class AppProperties {
@NotBlank
@Pattern(regexp = "^(.+):\\d+$")
private String forwardingEndpoint;
}
我的应用程序.java
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
我正在使用该属性的组件:
public MyComponent(@Value("${app.forwarding-endpoint}") String forwardingEndpoint) {
log.info("Forwarding endpoint {}", forwardingEndpoint);
}
我得到的错误是:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'app' to com.config.AppProperties failed:
Property: app.forwardingEndpoint
Value: null
Reason: must not be blank
我错过了什么?
【问题讨论】:
-
当应用程序不工作时你是如何运行它的?
标签: spring spring-boot