不可变@ConfigurationProperties
2022-08-31 17:20:50
是否有可能具有带有Spring Boot注释的不可变(最终)字段?下面的示例@ConfigurationProperties
@ConfigurationProperties(prefix = "example")
public final class MyProps {
private final String neededProperty;
public MyProps(String neededProperty) {
this.neededProperty = neededProperty;
}
public String getNeededProperty() { .. }
}
到目前为止,我尝试过的方法:
- 创建具有两个构造函数的类的 a
@BeanMyProps- 提供两个构造函数:空和带参数
neededProperty - 豆子是用
new MyProps() - 现场结果
null
- 提供两个构造函数:空和带参数
- 使用和提供豆类。
@ComponentScan@ComponentMyProps- 结果在
BeanInstantiationException->NoSuchMethodException: MyProps.<init>()
- 结果在
我让它工作的唯一方法是为每个非最终字段提供getter/setter。