May 12th, 2010, 11:35 AM
Where to initialise Spring in EJB Container?
Hopefully this isn't too daft a question, but I've drawn a major blank. My team and I currently use Spring dependency injection in various Spring MVC applications, without issue.
We want to begin doing similar within our service layer of stateless session EJBs (deployed in Weblogic), but I've been frustrated in my attempts to unearth a best-practice of where/how to initialise Spring.
Virtually all tutorials/samples are either...
a) web applications, wherein Spring is initialised alongside the servlet
b) simplistic examples that demonstrate calling....
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(AppConfig.class);
...from a static void main() method. Obviously, within an EJB container, I have no such 'static void main' to work from, nor a servlet to hitch my IOC initialisation onto.
So where should I create my JavaConfigApplicationContext? I've toyed with the idea of static initialistion blocks, setSessionContext() in our EJB base class, JVM hooks, bootstraping etc. But I don't want to go off the beaten track. Am I missing something simple?
Note* - it isn't the EJBs themselves I'm specifically looking to 'wire', but rather various bits'n'bobs lie beneath the EJBs. Therefore, the Spring documentation on "Enterprise Java Beans (EJB) integration" is not what I'm looking for. I just want to initialise Spring within an EJB container, but I have no scope (nor need, frankly) to refactor the EJBs themselves to, for example, extend classes like Spring's AbstractStatelessSessionBean.
Any advice, much appreciated,
May 12th, 2010, 02:39 PM
Well I still suggest the reference guide as that also explains how to initialize/use a context from EJBs, especially the part that explains about the EJB base classes. Basically it tells you about the BeanFactoryLocator (interface) and several implementations. That is basically in charge of loading the context.
You could load it (and reuse it within several other EJBs) in the ejbCreate or setSessionContext method. I would suggest the ejbCreate, because that feels better.
May 13th, 2010, 05:12 AM
Hey Marten, thanks for the reply.
May 30th, 2010, 06:30 AM
thanks marten You helped me a lot.