【发布时间】:2013-02-05 13:20:27
【问题描述】:
ALL,我是Java World EJB的新手,在阅读了名为Ejb3.0 in Action的好书后,我有一个关于Session Bean的问题。会话 bean 的 EJB 客户端是什么?假设我们有一些代码如下所示。
//EJB definition
import javax.ejb.Remote;
@Remote
public interface PlaceOrder {
..
void addItem(Long itemId);
Long confirmOrder();
..
}
@Stateful
public class PlaceOrderBean implements PlaceOrder {
private List<Long> items;
public PlaceOrderBean () {
items = new ArrayList<Long>();
}
public void addItem(Long itemId) {
items.add(itemId);
}
@Remove
public Long confirmOrder() {
Order order = new Order();
...
return order.getOrderId();
}
}
//Client
import javax.ejb.EJB;
public class PlaceOrderTestClient {
@EJB
private static PlaceOrder placeOrder1;
@EJB
private static PlaceOrder placeOrder2;
public static void main(String [] args) throws Exception {
System.out.println("Exercising PlaceOrder EJB...");
placeOrder1.addItem(new Long(200));
placeOrder1.addItem(new Long(201));
Long orderId = placeOrder1.confirmOrder();
System.out.println("Order confirmation number: " + orderId);
}
}
更新
EJB 客户端是指placeOrder1 和placeOrder2 还是主应用程序?这是否意味着多个 EJB 客户端?另一个问题是如果它在 Servlet 而不是 main 方法怎么办?谢谢。
【问题讨论】:
-
请更正,非静态变量placeOrder1不能直接调用到main方法中。即不能从静态上下文引用非静态变量或方法。
-
@Visruth CV,谢谢。已经完成了。