Spring MVC中WebApplicationContext的初始化时机、负责方及过程咨询
Hey there! Let’s walk through your questions about Spring MVC’s WebApplicationContext—I’ve debugged and worked through these flow details countless times, so I’ll break it down clearly.
When is WebApplicationContext initialized?
The WebApplicationContext gets initialized during the web application startup phase, triggered by your Servlet container (like Tomcat, Jetty, or Undertow). There are two key scenarios depending on how you configure Spring:
- If you use
ContextLoaderListener(traditional setup), the rootWebApplicationContextinitializes as soon as the Servlet container finishes bootstrapping the web app—before any requests are handled. - If you rely solely on
DispatcherServlet(or use modern annotation-based config), its associatedWebApplicationContexteither initializes on the first incoming request, or during app startup (you can force startup-time initialization with theload-on-startupparameter inweb.xmlor equivalent annotation config).
Who is responsible for initializing WebApplicationContext?
Two main players work together here:
- Servlet Container: It triggers the initialization process by calling lifecycle methods on Spring’s web components (like listeners or servlets) when the app starts.
- Spring’s Web Components:
ContextLoaderListener: This listener is in charge of creating and initializing the root WebApplicationContext—the parent context that holds shared beans (like data sources, service layer beans) across all servlets in the app.DispatcherServlet: EachDispatcherServletcreates its own child WebApplicationContext which inherits beans from the root context, but can also define its own MVC-specific beans (like controllers, view resolvers, handler mappings). For modern setups, classes likeAbstractAnnotationConfigDispatcherServletInitializerhandle both root and child context initialization programmatically.
Step-by-Step Initialization Process
Let’s break down the full flow, starting with a traditional ContextLoaderListener + DispatcherServlet setup:
Root WebApplicationContext Initialization (via ContextLoaderListener)
- The Servlet container starts and reads your
web.xml(or detects annotated config classes). It finds theContextLoaderListenerdefinition and calls itscontextInitialized()method. ContextLoader(the backend for the listener) creates an instance ofWebApplicationContext—by default, this isXmlWebApplicationContextfor XML config, orAnnotationConfigWebApplicationContextif you’re using Java-based config.- It loads the configuration sources: from the
context-paramnamedcontextConfigLocationinweb.xml, or scans the base packages you’ve specified in annotation config. - The context runs Spring’s core
refresh()method: this includes parsing bean definitions, instantiating beans, resolving dependencies, calling initialization callbacks (@PostConstruct,InitializingBean), and publishing context events. - Once initialized, the root context is stored in the
ServletContextusing the keyWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, so other components can access it.
Child WebApplicationContext Initialization (via DispatcherServlet)
- After the root context is ready, the Servlet container initializes the
DispatcherServlet(either on startup or first request). - The
DispatcherServletlooks up the root context from theServletContextand sets it as its parent. - It creates its own child
WebApplicationContext, loads its specific configuration (fromservlet-levelcontextConfigLocationinweb.xml, or the config classes you’ve mapped to it in annotation setups). - It runs the same
refresh()process as the root context, but only for its own bean definitions—any beans defined here override those in the root context with the same name. - The child context is stored in the
ServletContextunder a key specific to theDispatcherServlet(usually including the servlet’s name), so the servlet can use it to handle incoming requests.
For modern annotation-based setups (using AbstractAnnotationConfigDispatcherServletInitializer), this process is streamlined: you define root config classes (for shared beans) and servlet config classes (for MVC beans) in the initializer, and it handles creating both contexts automatically during app startup.
内容的提问来源于stack exchange,提问作者Keshav Shelar

