我想用構造函數參數注入一個CDI託管bean的實例。我已經註釋一個構造函數參數與@注入,例如: -帶參數的CDI注入,哪裏是實例?
@Stateless
public class ShoppingCart {
int capacity;
ShoppingCart(){};
@Inject
ShoppingCart(int capacity) {
this.capacity = capacity;
}
'
'
}
我如何可以注入一個實例購物到另一個CDI託管bean,這樣我其實有我的購物比如我可以調用其他方法? 相當於: -
ShoppingCart sc = new ShoppingCart(10);
所有的例子似乎剛剛訪問引用bean的屬性和不實際獲得bean本身,例如實例從3.7. Bean constructors
@SessionScoped
public class ShoppingCart implements Serializable {
private User customer;
@Inject
public ShoppingCart(User customer) {
this.customer = customer;
}
public ShoppingCart(ShoppingCart original) {
this.customer = original.customer;
}
ShoppingCart() {}
...
}
@ConversationScoped
public class Order {
private Product product;
private User customer;
@Inject
public Order(@Selected Product product, User customer) {
this.product = product;
this.customer = customer;
}
public Order(Order original) {
this.product = original.product;
this.customer = original.customer;
}
Order() {}
...
}
定單實例不以我的購物對象,它可以調用我的購物方法,最終,你最終會得到,如果你沒有
@Inject
ShoppingCart cart;
但當然,這就要求你使用setter方法在 容量值之後通過一個setter方法: -
cart.setCapacity = 10;
似乎有一些懷疑我是否真的有可能@注入一個具有構造函數參數的bean?我還沒有找到一個例子,任何幫助總是感激。
容量從哪裏來?它是配置嗎? –