2017-04-05 46 views
0

你好,我的問題是一個產品,例如在一個applicationcoped bean產生實例也applicationscaped?它是否需要它的類範圍或始終是依賴的?是否cdi生產者採取類作用域

+0

您可以檢查(含代碼):https://stackoverflow.com/questions/46559523/creating-application-scoped-class-member-with-a-producer –

回答

1

說明書對待督促作爲bean的ucer方法(基本上,生產者是定義的方式,如何創建給定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(); 
    } 
} 
+0

感謝我maked你的問題一樣漂亮好 – Ckkn

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"; 
    } 
}