cdi 生产者是否占用类范围



您好,我的问题是,例如在应用程序复制的bean上产生实例是否也产生应用程序范围的实例?它是采用其类范围还是始终依赖?

该规范将生产者方法视为 bean(基本上,生产者是你如何创建给定 bean 类型的实例的定义(。因此,适用一条规则,即如果未提供范围,则假定@Default

因此,您的问题的答案是 - 如果未指定生产者范围,则@Default。生产者范围和声明它的 Bean 的范围之间没有链接。

@ApplicationScoped
public MyBean {
  @Produces //this will produce @Dependent
  public Foo produceDependent() {
    return new Foo();
  }
  @Produces
  @RequestScoped //produces the scope you define
  public Bar produceReqScopedBean() {
    return new Bar();
  }
}

这取决于

产生@Dependent

@ApplicationScoped
class Bean {
    @Produces
    public String producesString(){
        return "test";
    }
}

产生@ApplicationScoped

@ApplicationScoped
class Bean {
    @Produces
    @ApplicationScoped
    public String producesString(){
        return "test";
    }
}

产生@RequestScoped

@ApplicationScoped
class Bean {
    @Produces
    @RequestScoped
    public String producesString(){
        return "test";
    }
}

最新更新