PDA

View Full Version : EJB vs Spring



springcleaning
Dec 1st, 2005, 09:37 AM
I am a new Spring user looking for an alternative to EJB. I found Spring. I see the many options available from the Spring framework and I understand the DAO Support and AOP/Declarative Transaction as an alternative to EJB Entity Beans using CMP but I don't see where Spring fills the gap for SessionBean (stateful or stateless) ? Does anyone know of some documentation that specifies an "EJB to Spring" roadmap or Spring technology alternatives for some of the high level functionality provided by EJB 2.0?

wpoitras
Dec 1st, 2005, 12:04 PM
It depends on what the requirements are.

Spring offers an alternative to local stateless session beans. But if you need remoting Spring also offers integration with remote stateless session beans, Hessian, Burlap, RMI, etc.

The basic paradigm of Spring services is:
- Define your business interface without any reliance on EJB APIs
- Implement your business object that implements the business interface.
- Your client code should look something like this:

ApplicationContext ctx;
MyBusinessInterface mbi = (MyBusinessInterface )ctx.getBean("myBean");
mbi.doMyBusiness();


How you do your implementation is seperate from the client side. You can choose to use the business object as a local object, possibly with AOP wrapped around it for transaction and other AOP purposes. Or you could create some sort of remoting. Only the configuration files would need to change. You code wouldn't change at all.

For more detail about Stateless and Stateful session bean support in Spring see this article (http://www.javaworld.com/javaworld/jw-02-2005/jw-0214-springejb.html)

For stateless EJBs you would have to implement a bean which called your business object. The AbstractStatelessSessionBean class would be a good start. You would also have to create a remote version of your business interface. But XDoclet can help you with that. Also, take a look at the reference manual about EJB. Also take a look at Rod Johnson's book J2EE Development without EJB.

I have seen less written about stateful session beans, but it looks like they would be implemented similar to stateless. You can use the convenience class AbstractStatefulSessionBean on the server side.

Ben Alex
Dec 7th, 2005, 03:03 PM
Generally services layer is your replacement for SLSBs and SFSBs. The big differences are your services layer will be POJOs that do not use the service locator pattern (ie JNDI lookups) - instead they are dependency injected. Further, services such as transactions and security are declaratively applied via AOP. SFSBs are usually avoided by storing session state in a session state holder such as HttpSession.

Don't extend AbstractStatefulSessionBean unless you're wanting to use an EJB. In this era there are very little reason to use EJB.