Micronaut setter 注入

Micronaut setter injection

Micronaut 没有在 setter 方法上注入 @Value。不支持吗?例如,我有

public class Example {


  @Value("${config.one}") //field injection works
  private String one;

  @Value("${config.two}") //field injection works
  private String two;

  @Value("${config.one}") //setter injection doesn't work
  public void setOne(String one) {
    this.one = one;
  }

  @Value("${config.two}") //setter injection doesn't work
  public void setTwo(String two) {
    this.two = two;
  }

}

你class一定是IoC容器的bean。获取 bean 的最简单方法是用 @javax.inject.Singleton

注释 class

您可以在此处查看示例 https://docs.micronaut.io/latest/guide/index.html#valueAnnotation

要使用 setter 注入,必须结合使用 @Inject@Property

  @Inject
  public void setOne(@Property(name = "config.one") String one) {
    this.one = one;
  }

必须注意一些问题。只需 CTRL + F Using the @Property AnnotationMicronaut Docs.