I have configured the root application context using ContextLoaderListener
and the context init-parameter contextConfigLocation
.
The root c
If you have a DispatcherServlet
running, there is no need to use ContextLoaderListener
. Just use ContextLoader.getCurrentWebApplicationContext()
to access the WebApplicationContext
.
Just keep the bean definitions separate as outlined in this previous answer.
To answer your first question, the DispatcherServlet creates a context because that's how it allows itself to be configured, and if you have multiple DispatcherServlets in one app, they'd each need to be configured separately. Therefore each one gets its own context, and each of those contexts is separate from the "root" context, where all of your real, "working" beans should live so they can be shared between the other contexts. There have been a number of questions over the last couple of weeks that were spawned by confusion over this very issue. You might gain a better understanding of how things work by checking out the answers:
Spring XML file configuration hierarchy help/explanation
Declaring Spring Bean in Parent Context vs Child Context
Spring-MVC: What are a "context" and "namespace"?