title: Spring: Why "root" application context and "servlet" application context are created by different parties?
body: <p>As I understand, a Spring based web application is initialized as below:</p>
  
  <p><strong>Step 1</strong>: <code>Servlet container (e.g. Tomcat)</code> locates the implementation of <code>ServletContainerInitializer</code>, which is <code>SpringServletContainerInitializer</code>.</p>
  
  <p><strong>Step 2</strong>: <code>SpringServletContainerInitializer</code> creates <code>DispatcherServlet</code> and <code>ContextLoaderListener</code></p>
  
  <p><strong>Step 3</strong>: <code>DispatcherServlet</code> creates <code>servlet application context</code>. And <code>ContextLoaderListener</code> creates <code>root application context</code>.</p>
  
  <p>Step 1 is defined by Servlet 3.0 spec. Step 2, 3 are totally defined by Spring.</p>
  
  <p>I can see the rational of putting <code>web</code> beans in <em>servlet context</em> and <code>non-web</code> beans in <em>root context</em>. But why do we have to create these 2 contexts in <em>different</em> places, i.e. <code>DispatcherServlet</code> and <code>ContextLoaderListener</code>?</p>
  
  <p>If <em>all</em> we want is <strong><em>just</em></strong> to prepare everything necessary, why not just create both contexts in <code>ContextLoaderListener</code> since it can be seen as the <code>main()</code> method of the whole web application. I think that's more logic and current approach only complicates things up.</p>
  
  <h2>ADD 1</h2>
  
  <p>Based on @Shailendra's reply, I draw this:</p>
  
  <p><a href="https://i.stack.imgur.com/VpijM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/VpijM.png" alt="enter image description here"></a></p>
  
  <p>My understanding is, Spring introduced the <code>application context</code> concepts and store them in the <code>Servlet Context</code>. Servlet Context is a concept introduced by java servlet technolgoy.</p>
  
  <p>I guess the <code>DispatcherServlet</code> implementation should have a member variable to hold the <code>key</code> to its <code>servlet application context</code> in the <code>servlet context</code>. So it can access it's own context. Maybe the key is the servlet name.</p>
  
  <p>And the <code>root application context</code> should have a <em>well-known</em> key so everyone can access it.</p>
  
  <h2>ADD 2</h2>
  
  <p>The <em>well-known</em> key for <code>root application context</code> is this:</p>
  
  <p>(in <code>org.springframework.web.context.WebApplicationContext</code>)</p>
  
  <pre><code>String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
  </code></pre>
  
  <h2>ADD 3</h2>
  
  <p>The <code>DispatcherServlet</code> does have a reference to its <code>WebApplicationContext</code>. It inherits the following memeber from <code>FrameworkServlet</code>:</p>
  
  <pre><code>/** WebApplicationContext for this servlet */
  private WebApplicationContext webApplicationContext;
  </code></pre>
  
  <p>And</p>
  
  <pre><code>public FrameworkServlet(WebApplicationContext webApplicationContext) {
      this.webApplicationContext = webApplicationContext;
  }
  </code></pre>