Thursday, October 12, 2006

How to properly create a Stateless Session Bean


Per standard EJB design patterns, a SSB should never have any business logic in its methods, all business logic should be delegated to a POJO provider. A POJO provider can be injected using Spring's AbstractStatelessSessionBean. Here is example code:


[import org.spring.*

public class BusinessSSB extends AbstractStatelessSessionBean implements BusinessInterface {
private BusinessInterface provider;

public void businessMethodOne() {
provider.businessMethod1();
}
// other business methods here...

protected void onEjbCreate() throws CreateException {
provider = (BusinessInterface) createProvider("BusinessProvider");
}

protected Object createProvider(String componentName) {
Object o = getBeanFactory().getBean(componentName);
return o;
}

}