从Spring Boot 2.2开始,最后可以定义一个用修饰的不可变类
@ConfigurationProperties。
该文档显示了一个示例。
您只需要声明一个带有绑定字段的构造函数(而不是setter方法),并
@ConstructorBinding在类级别添加注释以指示应使用构造函数绑定。
因此,您的实际代码没有任何二传手就可以了:
@ConstructorBinding@ConfigurationProperties(prefix = "example")public final class MyProps { private final String neededProperty; public MyProps(String neededProperty) { this.neededProperty = neededProperty; } public String getNeededProperty() { .. }}


