How can I make Spring-Boot fail during startup if any of the properties of a @ConfigurationProperties
class is missing?
If I use @Value(...)
instead, the auto-wiring fails as expected, but I want to centralise all properties into a @ConfigurationProperties
class instead of using @Value
references spread through the application.
The code:
@Component
@ConfigurationProperties(value = "serving.api", ignoreUnknownFields = false)
public class ApplicationProperties {
private String projectId;
private String bigTableInstanceId;
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getBigTableInstanceId() {
return bigTableInstanceId;
}
public void setBigTableInstanceId(String bigTableInstanceId) {
this.bigTableInstanceId = bigTableInstanceId;
}
}
You can achieve this by adding a @NotNull
validator to your mandatory properties. For example:
@NotNull
private String projectId;
public void setProjectId(String projectId) {
this.projectId = projectId;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With