You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC中WebApplicationContext的初始化时机、负责方及过程咨询

WebApplicationContext Initialization in Spring MVC: All You Need to Know

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 root WebApplicationContext initializes 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 associated WebApplicationContext either initializes on the first incoming request, or during app startup (you can force startup-time initialization with the load-on-startup parameter in web.xml or equivalent annotation config).

Who is responsible for initializing WebApplicationContext?

Two main players work together here:

  1. Servlet Container: It triggers the initialization process by calling lifecycle methods on Spring’s web components (like listeners or servlets) when the app starts.
  2. 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: Each DispatcherServlet creates 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 like AbstractAnnotationConfigDispatcherServletInitializer handle 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 the ContextLoaderListener definition and calls its contextInitialized() method.
  • ContextLoader (the backend for the listener) creates an instance of WebApplicationContext—by default, this is XmlWebApplicationContext for XML config, or AnnotationConfigWebApplicationContext if you’re using Java-based config.
  • It loads the configuration sources: from the context-param named contextConfigLocation in web.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 ServletContext using the key WebApplicationContext.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 DispatcherServlet looks up the root context from the ServletContext and sets it as its parent.
  • It creates its own child WebApplicationContext, loads its specific configuration (from servlet-level contextConfigLocation in web.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 ServletContext under a key specific to the DispatcherServlet (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:52:34