Java cdi制作人是否接受类范围

Java cdi制作人是否接受类范围,java,jakarta-ee,cdi,Java,Jakarta Ee,Cdi,您好,我的问题是,例如,应用程序范围bean上的product是否也会产生应用程序范围的实例?它是采用其类范围还是始终依赖 这取决于 产生@Dependent @ApplicationScoped class Bean { @Produces public String producesString(){ return "test"; } } 生成@ApplicationScoped @ApplicationScoped class Bean {

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

这取决于

产生@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";
    }
}

规范将producer方法视为bean(基本上,producer是如何创建给定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();
  }
}
您可以(使用代码)检查: