2017-03-02 84 views
1

如何在启动过程中使Spring-Boot失败,如果缺少@ConfigurationProperties类的任何属性?如果属性丢失,Spring Boot不会失败

如果我使用@Value(...)代替,自动布线会按预期失败,但我想集中所有属性到@ConfigurationProperties类中,而不是使用通过应用程序传播的@Value引用。

代码:

@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; 
    } 

} 

回答

3

您可以通过添加一个@NotNull验证您的强制属性实现这一目标。例如:

@NotNull 
private String projectId; 

public void setProjectId(String projectId) { 
    this.projectId = projectId; 
}