【发布时间】:2012-12-16 13:21:47
【问题描述】:
我正在学习 Java EE 和 JSP。我在 NetBeans 中创建了一个企业应用程序项目。
我有一个 EJB project 和一个 WAR project ,其中所有的 bean 都在其中。
我的问题是注释 @EJB 没有在 WAR 应用程序中实例化我的 Bean。我可以在 EJB 应用程序之外使用@EJB 吗?
在 EJB 项目中,我有这些文件:
CustomerRemote.java
@Remote
public interface CustomerRemote {
public Customer createCustomer();
public Customer getCustomer(int customerId);
public void removeCustomer();
}
CustomerBean.java
@Stateless
public class CustomerBean implements CustomerRemote {
@PersistenceContext(unitName="testPU")
private EntityManager entityManager;
@Override
public Customer createCustomer() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void removeCustomer() {
}
@Override
public Customer getCustomer(int customerId) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
在 WAR 项目中,我有一个文件,我的 JSP 页面使用它来与 EJB 进行通信。问题是 CustomerRemote 对象永远不会被实例化。 @EJB 注释似乎不起作用,因为customerRemote 始终为空。但是当使用lookup 方法实例化它时,它可以工作!那么为什么@EJB 不起作用呢?
public class CustomerProxyBean {
@EJB
private CustomerRemote customerRemote;
public CustomerProxyBean() {
// try {
// InitialContext context = new InitialContext();
// customerRemote = (CustomerRemote)context.lookup("java:app/SimpleShop-ejb/CustomerBean");
//
// } catch (NamingException ex) {
// Logger.getLogger(CustomerProxyBean.class.getName()).log(Level.SEVERE, null, ex);
// }
}
【问题讨论】: