Simplified Session Beans
In EJB 2.x, a session bean is required to implement the SessionBean
interface. An EJB 3.0 session bean class is a POJO (Plain Old Java Object) and does not implement the SessionBean
interface.
An EJB 2.x session bean class includes one or more ejbCreate
methods, the callback methods ejbActivate, ejbPassivate, ejbRemove
, and setSessionContext
, and the business methods defined in the local/remote interface. An EJB 3.0 session bean class includes only the business methods.
In EJB 3.0, EJB component interfaces and home interfaces are not required for session beans. A remote interface in an EJB 2.x session EJB extends the javax.ejb.EJBObject
interface; a local interface extends the javax.ejb.EJBLocalObject
interface. A home interface in an EJB 2.x session EJB extends the javax.ejb.EJBHome
interface; a local home interface extends the javax.ejb.EJBLocalHome
interface. In EJB 3.0 the home/local home and remote/local interfaces are not required. The EJB interfaces are replaced with a POJI (Plain Old Java Interface) business interface. If a business interface is not included with the session bean class, a POJI business interface gets generated from the session bean class by the EJB server.
An EJB 2.x session EJB includes a deployment descriptor that specifies the EJB name, the bean class name, and the interfaces. The deployment descriptor also specifies the bean type of Stateless/Stateful. In EJB 3.0, a deployment descriptor is not required for a session bean. An example EJB 2.x session bean, which implements the SessionBean
interface, is listed next:
import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class CatalogBean implements SessionBean { private SessionContext ctx; public String getJournal(String publisher) { if (publisher.equals("Oracle Publisher")) return new String("Oracle Magazine"); if (publisher.equals("OReilly")) return new String("dev2dev"); } public void ejbCreate() { } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } }
In EJB 3.0, metadata annotations are used to specify the session bean type and local and remote business interfaces. A stateless session bean is specified with the annotation @Stateless
, a stateful session bean with the annotation @Stateful
. Component and home interfaces are not required for a session bean. A session bean is required to implement a business interface. The business interface, which is a POJI, may be a local or remote interface. A local interface is denoted with the annotation @Local
and a remote interface is denoted with the annotation @Remote
. A session bean may implement one or both (local and remote) of the interfaces. If none of the interfaces is specified, a local business interface gets generated. The remote and local business interface class may be specified in the @Local
and @Remote
annotations. For example, a local business interface may be specified as @Local ({CatalogLocal.class})
.
The EJB 3.0 session bean corresponding to the EJB 2.x stateless session bean is annotated with the metadata annotation @Stateless
. The EJB 3.0 bean class does not implement the SessionBean
interface. The EJB 3.0 session bean implements a business interface. The @Local
annotation specifies the local business interface for the session bean. The EJB 3.0 session bean corresponding to the EJB 2.x example session bean is listed next:
import javax.ejb.*; @Stateless @Local( { CatalogLocal.class }) public class CatalogBean implements CatalogLocal { public String getJournal(String publisher) { if (publisher.equals("Oracle Publisher")) return new String("Oracle Magazine"); if (publisher.equals("OReilly")) return new String("java.net"); } }
In EJB 3.0, the component and home interfaces of EJB 2.x are replaced with a business interface. The business interfaces for the session bean are POJIs, and do not extend the EJBLocalObject
or the EJBObject
. A local business interface is denoted with the annotation @Local
. A remote business interface is denoted with the annotation @Remote
. A remote business interface does not throw the RemoteException
. The local business interface corresponding to the session bean class is listed next:
import javax.ejb.*; @Local public interface CatalogLocal { public String getJournal(String publisher); }
A client for an EJB 2.x session bean gets a reference to the session bean with JNDI. The JNDI name for the CatalogBean
session bean is CatalogLocalHome
. The local/remote object is obtained with the create()
method. The client class for the EJB 2.x session bean is listed.
import javax.naming.InitialContext; public class CatalogBeanClient { public static void main(String[] argv) { try { InitialContext ctx = new InitialContext(); Object objref = ctx.lookup("CatalogLocalHome"); CatalogLocalHome catalogLocalHome = (CatalogLocalHome) objref; CatalogLocal catalogLocal = (CatalogLocal) catalogLocalHome .create(); String publisher = "OReilly"; String journal = catalogLocal.getJournal(publisher); System.out.println("Journal for Publisher: " + publisher + " " + journal); } catch (Exception e) { System.err.println(e.getMessage()); } } }
In EJB 3.0, a reference to a resource may be obtained with a dependency injection with the @EJB
annotation. JNDI lookup and create()
method invocation is not required in EJB 3.0. The client class for the EJB 3.0 session bean is listed next:
public class CatalogClient { @EJB CatalogBean catalogBean; String publisher="OReilly"; String journal=catalogBean.getJournal(publisher); System.out.println("Journal for Publisher: "+publisher +" "+journal); }