title: SpringMVC app... why are app contexts being initialized twice?
body: <p>I've seen this basic question asked on these forums before, but none of the answers seem to solve my particular issue. Anyway, I have a small Spring webapp, consisting of a core Spring business layer, SpringMVC, and Spring-Quartz (I am also using MyBatis, although I don't believe that's relevant). All of my Spring libraries are 3.1.3</p>
  
  <p>The problem is that when I deploy my app to Tomcat 6, usually the root application context and the web application context are each being initialized twice. This is evident by looking at the logs, and is also evident by the fact that my Quartz job is firing off twice when it should be firing once (that latter point is why this is not just a theoretical problem).</p>
  
  <p>I thought I had my application contexts all sorted out, but clearly there must be something that I'm missing. My general approach is this:</p>
  
  <ol>
  <li>Put basic Spring configuration in <em>spring-biz-context.xml</em>, in the classpath (i.e. it winds up in WEB-INF/classes/com/me/config)</li>
  <li>Put Spring Quartz configuration in <em>spring-quartz-context.xml</em>, in the class path (i.e. it also winds up in WEB-INF/classes/com/me/config)</li>
  <li>Place an <em>applicationContext.xml</em> file in WEB-INF; have it simply import the above two XML files</li>
  <li>Declare a ContextLoaderListener in <em>web.xml</em>, so that the webapp will seek out the abovementioned <em>applicationContext.xml</em> file, thus initializing the root context.</li>
  <li>Declare a servlet (of type DispatcherServlet) called <em>UsMain</em> in <em>web.xml</em></li>
  <li>Create a <em>UsMain-servlet.xml</em> file within WEB-INF, containing some minimal stuff just for the web application context.</li>
  </ol>
  
  <p>Below are my config files, minus all of the XML cruft:</p>
  
  <p>UsMain-servlet.xml</p>
  
  <pre><code>    &lt;bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  
  &lt;mvc:resources mapping="/rsc/**" location="/rsc/" cache-period="31556926"/&gt;
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;context:component-scan base-package="com.me.controllers" /&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;web-app ... version="2.5"&gt;
  ...
  
    &lt;servlet&gt;
  &lt;servlet-name&gt;UsMain&lt;/servlet-name&gt;
  &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
    &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;UsMain&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  &lt;listener&gt;  
      &lt;listener-class&gt;  
          org.springframework.web.context.ContextLoaderListener  
      &lt;/listener-class&gt;  
  &lt;/listener&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>applicationContext.xml</p>
  
  <pre><code>    &lt;import resource="classes/com/me/config/spring-biz-context.xml" /&gt;
  &lt;import resource="classes/com/me/config/spring-quartz-context.xml" /&gt;
  </code></pre>
  
  <p>Below are a sampling of log lines that I see <em>twice</em> when Tomcat starts up:</p>
  
  <pre><code>2013-11-07 05:18:27 [main] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
  2013-11-07 05:18:27 [main] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Thu Nov 07 05:18:27 UTC 2013]; root of context hierarchy
  2013-11-07 05:18:27 [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
  2013-11-07 05:18:35 [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/com/me/config/spring-biz-context.xml]
  ... bunch of stuff ...
  2013-11-07 05:18:38 [main] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'UsMain-servlet': startup date [Thu Nov 07 05:18:38 UTC 2013]; parent: Root WebApplicationContext
  2013-11-07 05:18:38 [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/UsMain-servlet.xml]
  </code></pre>
  
  <p>The one other thing which may or may not be a red herring... if I modify my code or config files in any way and then repackage the WAR file, and then stop Tomcat, copy the WAR into webapps/, and start Tomcat, the app contexts load only once (yay!) But if I restart Tomcat after that, then this problem occurs.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As I keep exploring this issue, the "may or may not be a red herring" thing seems to be an important factor. In fact, this may be more of a question about Tomcat than about Spring. </p>
  
  <p>If I delete the deployed webapp's subdirectory from within the webapps/ directory before restarting Tomcat, then the problem does <strong>not</strong> occur. If I leave the subdirectory intact before restarting Tomcat, then the problem <strong>does</strong> occur.</p>
  
  <p>In other words, I package the webapp into MyApp.war, and stick that WAR into Tomcat's webapps/ directory. When Tomcat starts, it unpacks the WAR and creates a webapps/MyApp/ subdirectory. The first time, things work fine. But if I just restart Tomcat, the problem occurs. If I delete webapps/MyApp/ and <em>then</em> restart Tomcat, then things are again fine.</p>
  
  <p>So, in a way, the problem is solved. But ideas on what is happening would be still be very helpful. To my knowledge, this is not the way Tomcat/webapps were intended to operate.</p>