cdi生产者是否采用类范围 [英] Does cdi producer take class scope
问题描述
您好我的问题是,例如,在applicationcoped bean上生成实例还是applicationcoped吗?它是否需要其类范围或始终依赖?
Hello my question is does a produce for example on a applicationscoped bean produces instances also applicationscoped? Does it take its class scope or is always dependent?
推荐答案
规范将生产者方法视为bean(基本上,生产者是如何创建给定实例的定义bean类型)。因此,规则适用,如果没有提供范围,则假定 @Default
。
The specification treats producer methods as beans (basically, producer is a definiton of how you create an instance of given bean type). Therefore a rule applies, that if no scope is provided, @Default
is assumed.
因此您的答案问题是 - 生产者范围是 @Default
如果没有指定。生产者范围与声明它的bean范围之间没有联系。
Hence the answer to your question is - the producer scope is @Default
if none is specified. There is no link between producer scope and the scope of the bean on which it is declared.
@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();
}
}
这篇关于cdi生产者是否采用类范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!