我想用构造函数参数注入一个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?我还没有找到一个例子,任何帮助总是感激。
容量从哪里来?它是配置吗? –