I have 3 application contexts:
- A global context which is responsible for launching Jetty servlet container, launched from main()
- An applicationContext parent to each webapp in jetty (started by SpringSecurity
- n xyz-servlet contexts for each webapp (I run 1 webapp)
Spring managed creating a parent-child relationship for the latter two contexts.
Now I would like to add a simple global properties service to the first context and have it visible to the others (I could add it to applicationContext, but I may use it elsewhere outside the webapp context later and want to plan for that).
Since I didn’t manage creation of the other two contexts (they were created by the dispatcher servlet and spring security which were launched from web.xml) I’m not clear how and where I should access them to define the first as the parent (well, only applicationContext should take the global context as its parent).
The parent-child relationship between (1) and (2) can be managed by the
ContextLoaderListenerthat you have in yourweb.xml.Specifically, have a look at the javadoc for
ContextLoader.loadParentContext(). This documents how theContextLoaderListenercan locate context (1). It assumes that this global context was initialised viaContextSingletonBeanFactoryLocator, which you may or may not have used to create that context.If you did use
ContextSingletonBeanFactoryLocator, then it should be trivial, just follow the instructions in the javadoc. if you used some other way of bootstrapping the global context, then you can subclassContextLoaderListener, override theloadParentContext()method to locate your global context, then use that inweb.xmlinstead of the standardContextLoaderListener.