是否可以使用Spring Boot的@ConfigurationProperties注释创建不可变(最终)字段?以下示例
@ConfigurationProperties(prefix = "example")
public final class MyProps {
private final String neededProperty;
public MyProps(String neededProperty) {
this.neededProperty = neededProperty;
}
public String getNeededProperty() { .. }
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试过的方法:
@Bean了的MyProps两个类的构造函数
neededPropertyargumentnew MyProps()null@ComponentScan和@Component提供MyPropsbean.
BeanInstantiationException- >NoSuchMethodException: MyProps.<init>()我得到它的唯一方法是为每个非最终字段提供getter/setter.