I’m using Servlet 3 (Tomcat 7) + Spring 3.1, and trying to load my webapp using WebApplicationInitializer.
In the usual examples I’ve seen, you have a Root ApplicationContext, loaded with ContextLoaderListener, and a servlet ApplicationContext, loaded with DispatcherServlet.
(To be clear, I’m not talking about web.xml, but programatically, inside WebApplicationInitializer).
Now, I’d like to have a hierarchy of ApplicationContexts, let’s say:
Root -> AppContext1 -> AppContext2 -> ServletAppContext
-> denotes Parent -> Child relationship. Each AppContext can access its own beans and the ones of its ancestors.
For an example:
- Root defines properties, DAO and TX.
- AppContext1 defines JPA and Spring Data repositories.
- AppContext2 defines JMS and Spring integration pipes.
- ServletAppContext defines Controllers and views.
My first approach was to add the Root ApplicationContext to the ContextLoaderListener, Then set it as a parent of AppContext1. Set AppContext1 as a parent of AppContext2. Set AppContext2 as a parent of ServletAppContext. And finally associate ServletAppContext with the DispatcherServlet.
The thing is, at shutdown time, DispatcherServlet closes ServletAppContext, but it does not propagate. AppContext1 and AppContext2 are never closed to their beans are never released. So I’m guessing I’m using a wrong approach.
I tried associating AppContext2 to ContextLoaderListener instead of Root. In this case, AppContext2 closes, but AppContext1 and Root remain open.
I also cannot have 3 ContextLoaderListeners, 1 for each of my AppContexts (Root, 1, 2).
My question is, what’s the right approach for this situation? I’m open to suggestions.
So, clearly I’m trying to do something Spring (at least up to version 3.1) is not prepared to do.