Sample EJB Configs for Spring
For legacy reasons, mandated technology reasons, or the need to do clustered remote access, a number of people do need to use EJBs with Spring, whether they want to or not.
The existing ejbtest integration test app under the autobuilds directory in the Spring source tree was originally created to be used for integration level testing of Spring code which couldn’t be properly tested with only a unit test, since it needed an EJB (or alternately J2EE Application, i.e. what an EAR file represents) environment to run in.
Since Spring currently doesn’t ship with any EJB based sample apps, and a sample goes a long way towards making explanations in the manual clearer, I spent a few hours the other day making the ejbtest app also usable as an example of how you can configure and access EJBs in a Spring environment, with the following variations (all running with JTA coordinating transactions):
- EJB is set to use CMT (Container Managed Transactions). It delegates to a POJO service object which is itself wrapped with Spring transactions, and ultimately performs some Hibernate based operations.
- EJB is set to NOT use CMT (it is non-transactional). It delegates to a POJO service object which is itself wrapped with Spring transactions, and ultimately performs some Hibernate based operations.
- EJB is set to use CMT. It delegates to a POJO service object which is NOT wrapped by Spring transactions, and ultimately performs some Hibernate based operations. What is interesting about this case is that as long as Hibernate is configured (via the SessionFactory) to be able to do a lookup of the JTA TransactionManager in the appserver environment, Spring is also able to get at this TM, and create and bind Hibernate Sessions to the JTA/CMT transaction, even in the absence of a Spring transaction manager.
One caveat to anybody looking at the config: the bean names in there are choosen to make it clear what is being tested, and as such include things like the transaction strategy. Obviously in a real-life app, you would not put something like this in the name of a bean, since the whole point is that this sort of thing should be swappable without affecting other beans or clients.
Anybody wanting to look at the config should check out the Spring source, and look under ‘/autobuilds/apps/ejbtest’. Look for the SimpleEJB variations. Alternately, you can browse the CVS tree here.
I’ve been thinking we should just ship an EJB-based sample app with Spring, but I don’t think there’s enough time for this to happen in the 1.1 timeframe.


Modified
