cdi生产者是否采用类范围

时间:2017-04-05 22:11:29

标签: java java-ee cdi

您好我的问题是,例如,在applicationcoped bean上生成实例还是applicationcoped吗?它是否需要其类范围或始终依赖?

2 个答案:

答案 0 :(得分:2)

规范将生产者方法视为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();
  }
}

答案 1 :(得分:1)

取决于

制作@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";
    }
}