技术开发 频道

EJB的入门教材


  【IT168技术文档】

   1、EJB的开发

  先泛泛而论,讲一讲EJB的开发步骤。

  1.1 SessionBean的开发

  第一步, 写远程接口(remote interface),继承EJBObject接口,把需要调用的public方法写在里面(这些方法将在SessionBean中实现),注意要声明throws java.rmi.RemoteException。

  例如:

package jsper.ejb; import java.rmi.*; import javax.ejb.*; public interface MyEJB extends EJBObject { public String sayHello() throws java.rmi.RemoteException; }
  第二步, 写Home接口(生成EJBObject引用的factory)

  至少生成一个create方法, 注意要声明throws java.rmi.RemoteException和javax.ejb.CreateException。

  比如:
package jsper.ejb; import java.rmi.*; import javax.ejb.*; public interface MyEJBHome extends EJBHome { MyEJB create() throws java.rmi.RemoteException, javax.ejb.CreateException; }
  第三步, 写真正的Session Bean的实现(实现定义在远程接口中的方法), 需要实现javax.ejb.SessionBean接口

  注意:不能用implents MyEJB的方式直接实现远程接口,此处不用抛出RemoteException
package jsper.ejb; import java.rmi.RemoteException; import javax.ejb.*; public class MyEJBClass implements SessionBean {    public MyEJBClass() {  }  //定义在SessionBean 中的方法  public void ejbCreate() throws RemoteException, CreateException {  }  public void ejbActivate() throws RemoteException {  }  public void ejbPassivate() throws RemoteException {  }  public void ejbRemove() throws RemoteException {  } public void setSessionContext(SessionContext ctx) throws RemoteException {  }  //此处是具体的实现  public String sayHello()  {   System.out.println("Hello");  } }
  第四步,写一个发布用的配置文件ejb-jar.xml

  需要提供的信息:
  Bean Home name -- The nickname that clients use to lookup your bean’s home object.
  Enterprise bean class name -- The fully qualified name of the enterprise bean class.
     Home interface class name
     Remote interface class name
        Re-entrant -- Whether the enterprise bean allow re-entrant calls. This setting must be false for session beans(it applies to entity beans only)
     stateful or stateless
    Session timeout -- The length of time (in seconds) before a client should time out when calling methods on your bean.
  最后你还可以提供属于自己的配置信息供自己控制EJB的工作方式。

  例子:
helloEjb
com.jsper.ejb.MyEJBHome
com.jsper.ejb.MyEJB
com.jsper.ejb.MyEJBClass
Stateless
Container   





0
相关文章