title: Spring MVC web app: application context starts twice
body: <p>I'm working on a Spring MVC REST API. Everything works fine, which is great, but I noticed from the logs that every time I restart my app the applicationContext loads twice: once when tomcat loads the war file, and a second time when the web app is accessed for the first time by a client. </p>
  
  <p>I'll give a few examples:</p>
  
  <p>Right after I start the tomcat:</p>
  
  <pre><code>Apr 11, 2013 10:14:35 AM org.apache.catalina.core.StandardEngine start
  INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
  Apr 11, 2013 10:14:36 AM org.apache.catalina.core.ApplicationContext log
  INFO: Initializing Spring root WebApplicationContext
  2013-04-11 10:14:36 INFO  ContextLoader:273 - Root WebApplicationContext:     initialization started
  2013-04-11 10:14:36 INFO  XmlWebApplicationContext:510 - Refreshing Root     WebApplicationContext: startup date [Thu Apr 11 10:14:36 EDT 2013]; root of context hierarchy
  2013-04-11 10:14:36 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions     from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
  2013-04-11 10:14:36 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-general.xml]
  2013-04-11 10:14:37 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-db.xml]
  2013-04-11 10:14:37 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-security.xml]
  2013-04-11 10:14:37 INFO  SpringSecurityCoreVersion:33 - You are running with Spring Security Core 3.1.3.RELEASE
  2013-04-11 10:14:37 INFO  SecurityNamespaceHandler:59 - Spring Security 'config' module version is 3.1.3.RELEASE
  </code></pre>
  
  <p>...</p>
  
  <p>And then at the moment I do the first API call:</p>
  
  <pre><code>INFO: Initializing Spring FrameworkServlet 'mvc-dispatcher'
  2013-04-11 10:15:25 INFO  DispatcherServlet:455 - FrameworkServlet 'mvc-dispatcher': initialization started
  2013-04-11 10:15:25 INFO  XmlWebApplicationContext:510 - Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Thu Apr 11     10:15:25 EDT 2013]; parent: Root WebApplicationContext
  2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
  2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-general.xml]
  2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-db.xml]
  2013-04-11 10:15:25 INFO  XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [config-security.xml]
  2013-04-11 10:15:25 INFO  SecurityNamespaceHandler:59 - Spring Security 'config' module version is 3.1.3.RELEASE
  </code></pre>
  
  <p>Surely this can't be normal behavior?? My web.xml looks like this:</p>
  
  <p>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"></p>
  
  <pre><code>&lt;display-name&gt;REST API&lt;/display-name&gt;
  
  &lt;!-- Servlets --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- filters --&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;httpMethodFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.HiddenHttpMethodFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;httpMethodFilter&lt;/filter-name&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;etagFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.ShallowEtagHeaderFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;etagFilter&lt;/filter-name&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;CompressingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.planetj.servlet.filter.compression.CompressingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;debug&lt;/param-name&gt;
          &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;statsEnabled&lt;/param-name&gt;
          &lt;param-value&gt;false&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;CompressingFilter&lt;/filter-name&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  
  
  &lt;!-- listeners --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>