调用 Enterprise Bean 是指获取对 Enterprise Bean 的引用以调用其方法的过程。要调用某个 Enterprise Bean 的方法,您需要创建该 Bean 的本地或远程接口的实例,然后调用接口的方法。
要自动为 Enterprise Bean 生成查找代码(包括任何部署描述符配置代码),请右键单击 Java 文件,然后选择“企业资源”>“调用 Enterprise Bean”。有关详细信息,请参见调用 Enterprise Bean。
对于 Java EE 5 应用程序和 J2EE 1.4 应用程序来说,Enterprise Bean 的调用过程是不同的。
@EJB private MyEJBInterface newMyEJB;
如果要在不同的项目中调用同一个 Enterprise Bean,还必须将 EJB 项目添加到调用该 Enterprise Bean 的项目的类路径中。
private MySessionRemote lookupMySessionBean() { try { Context c = new InitialContext(); Object remote = c.lookup("java:comp/env/ejb/MySessionBean"); MySessionRemoteHome rv = (MySessionRemoteHome) PortableRemoteObject.narrow(remote, MySessionRemoteHome.class); return rv.create(); } catch exceptions
然后,必须使用类似于以下内容的代码来获取接口实例:
private MySessionRemote newMySessionRemote = lookupMySessionBean();
最后,在从中调用 Enterprise Bean 的模块的部署描述符中注册 EJB 引用。是否添加引用以及添加引用的位置取决于是否符合下面的相应条件:
注意: