cdi生产者是否采用类范围
您好我的问题是,例如,在applicationcoped bean上生成实例还是applicationcoped吗? 它是否需要其类范围或始终依赖?
规范将生产者方法视为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"; } }