因为教学上的需要,重新梳理了下EJB几种接口的职能,讲的是EJB3,虽然按照课件也能做出一个运行良好的EJB程序来,但是要想比较好的理解EJB3的工作原理,只知道这些注解还是不够的,特别是涉及到的接口(home和remote)。
以下对这些接口做了一些总结:
Home接口
|
EJBHome:负责创建远程EJB对象(即实现了EJBObject接口的Remote对象)
|
public interface HaiRemoteHome extends EJBHome { HaiRemote create() throws RemoteException, CreateException; }
|
EJBLocalHome:负责创建本地EJB对象(即实现了EJBLocalObject接口的Remote对象)
|
|
Remote接口:里面定义我们的业务方法
|
EJBObject : 仅用于远程调用的业务接口
|
public interface HaiRemote extends EJBObject { String sayHai() throws RemoteException; }
|
EJBLocalObject : 仅用于本地调用的业务接口
|
|
SessionBean
|
会话bean:定义Bean的生命周期方法和实现Remote接口中定义的方法
|
public class HaiBean implements SessionBean { public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException { System.out.println("set session context"); } public void ejbCreate() throws EJBException { System.out.println("ejb create"); } public void ejbRemove() throws EJBException, RemoteException { System.out.println("ejb remove"); } public void ejbActivate() throws EJBException, RemoteException { System.out.println("ejb activate"); } public void ejbPassivate() throws EJBException, RemoteException { System.out.println("ejb passivate"); } public String sayHai() throws RemoteException { return "Hai, EJB 2!"; } }
|