相关疑难解决方法(0)

不可变的@ConfigurationProperties

是否可以使用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)

到目前为止我尝试过的方法:

  1. 创建@Bean了的MyProps两个类的构造函数
    • 提供两个构造函数:empty和with neededPropertyargument
    • bean是用.创建的 new MyProps()
    • 在该领域的结果 null
  2. 使用@ComponentScan@Component提供MyPropsbean.
    • 结果BeanInstantiationException- >NoSuchMethodException: MyProps.<init>()

我得到它的唯一方法是为每个非最终字段提供getter/setter.

java spring properties-file spring-boot

37
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×1

properties-file ×1

spring ×1

spring-boot ×1