Java Reference
In-Depth Information
public interface PostageServiceHome extends EJBHome {
public PostageServiceRemote create() throws RemoteException, CreateException;
}
If you want to expose this EJB component for local access within an enterprise application, the
preceding two interfaces should extend EJBLocalObject and EJBLocalHome instead, whose methods
don't need to throw RemoteException . For simplicity's sake, I'm omitting the local and local home
interfaces here.
Note that the following EJB implementation class also implements the PostageService business
interface so that you can delegate requests to the POJO service implementation.
package com.apress.springenterpriserecipes.post;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class PostageServiceBean implements SessionBean, PostageService {
private PostageService postageService;
private SessionContext sessionContext;
// this isn't part of the interface, but is required
public void ejbCreate() {
postageService = new PostageServiceImpl();
}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public double calculatePostage(String country, double weight) {
return postageService.calculatePostage(country, weight);
}
}
In the ejbCreate() life cycle method, you instantiate the POJO service implementation class. It's up
to this object to perform the actual postage calculation. The EJB component just delegates requests to
this object.
The astute reader will note that the ejbCreate() method is nowhere to be found on the SessionBean
interface. Instead, it's a convention. Stateless session beans can contain one version of ejbCreate() . If
the method has any arguments, the corresponding create method on the EJBHome bean must have the
same arguments. The ejbCreate() method is the EJB hook for initialization of state, much as a JSR-250
annotated @PostConstruct() method or afterPropertiesSet() method work in Java EE 5 and Spring. If
you have a stateful session bean, then there may be multiple overloaded ejbCreate() methods. Similarly,
for each overloaded form of ejbCreate() on the SessionBean , there must be a create method with the
same arguments on the EJBHome . We mention all this (which, if we're honest, doesn't even begin to cover
the nuances involved) to put in stark relief the Spring container's lightweight approach and to show that
even the Spring abstractions for EJB 2.x are incredible improvements.
Search WWH ::




Custom Search