Java Reference
In-Depth Information
Creating EJB 2.x Components with Spring's Support
As you can see, your EJB implementation class needs to implement all EJB life cycle methods even if you
don't need them. It should extend Spring's EJB support class to get the life cycle methods implemented
by default. The support class for stateless session beans is
AbstractStatelessSessionBean
.
■
Note To use Spring's EJB support for your EJB implementation classes, you have to include a few Spring
framework jars, including
spring-beans
,
spring-core
,
spring-context
,
spring-context-support
,
spring-asm
, and
spring-expression
in the classpath of your EJB container. For OpenEJB, you can copy these
JAR files to the
lib
directory of the OpenEJB installation directory. If your OpenEJB container is running, you will
have to restart it. In our testing, there was a conflict between the Spring 3 milestone releases we were using and
the
openejb-cxf j
ar in the lib directory of the OpenEJB installation. You may simply remove that jar or ignore the
stack trace in the console at startup.
package com.apress.springenterpriserecipes.post;
import javax.ejb.CreateException;
import org.springframework.ejb.support.AbstractStatelessSessionBean;
public class PostageServiceBean extends AbstractStatelessSessionBean
implements PostageService {
private PostageService postageService;
protected void onEjbCreate() throws CreateException {
postageService = (PostageService)
getBeanFactory().getBean("postageService");
}
public double calculatePostage(String country, double weight) {
return postageService.calculatePostage(country, weight);
}
}
When you extend the
AbstractStatelessSessionBean
class, your EJB class no longer needs to
implement any EJB life cycle methods, but you can still override them if necessary. Note that this class
has an
onEjbCreate()
method that you must implement to perform initialization tasks. Here, you just
retrieve the
postageService
bean from the Spring IoC container for this EJB component to use. Of
course, you must define it in a bean configuration file. This file can have an arbitrary name, but must be
located in the classpath. For example, you can create it as
beans-ejb.xml
in the root of the classpath.
Search WWH ::
Custom Search