title: Application context being loaded twice - Spring Boot
body: <p>I have a fairly simple setup. A maven project with 3 modules : core/webapp/model. I'm using Spring boot to gear up my application. In webapp, i have a simple class WebappConfig as follows:</p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan(excludeFilters = @ComponentScan.Filter(Configuration.class))
  public class WebappConfig {
  
      public static void main(String[] args) {
          SpringApplication app = new SpringApplication(WebappConfig.class);
          app.setAdditionalProfiles("dev");
          app.run(args);
      }
  }
  </code></pre>
  
  <p>and few classes in core/model module. My container-application point is :</p>
  
  <pre><code>public class AbcdXml extends SpringBootServletInitializer {
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(WebappConfig.class);
      }
  
  }
  </code></pre>
  
  <p>And no web.xml! My model's pom has following spring boot related dependency :</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Core's pom.xml :</p>
  
  <pre><code>&lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-batch&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Now running WebappConfig via Run as -> Java application works perfectly but i need to deploy the project as a war on tomcat7. Webapp's packaging is war. There is no tomcat provided jar's in lib except tomcat-jdbc and tomcat-tuli jar(Shouldn't be an issue?).</p>
  
  <p>When i deploy my abcd.war, applicationcontext is getting loaded twice and result in following error stracktrace :</p>
  
  <pre><code>2014-06-27 11:06:08.445  INFO 23467 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/abcd]        : Initializing Spring embedded WebApplicationContext
  2014-06-27 11:06:08.446  INFO 23467 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 19046 ms
  2014-06-27 11:06:21.308  INFO 23467 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
  2014-06-27 11:06:21.313  INFO 23467 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'errorPageFilter' to: [/*]
  2014-06-27 11:06:21.314  INFO 23467 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  2014-06-27 11:06:26.073  INFO 23467 --- [ost-startStop-1] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
  2014-06-27 11:06:26.127  INFO 23467 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
      name: default
      ...]
  2014-06-27 11:06:26.511  INFO 23467 --- [ost-startStop-1] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.1.Final}
  2014-06-27 11:06:26.521  INFO 23467 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
  2014-06-27 11:06:26.527  INFO 23467 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
  //some info messages from spring boot
  2014-06-27 11:07:31.664  INFO 23467 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-06-27 11:07:33.095  INFO 23467 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-06-27 11:07:33.096  INFO 23467 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2014-06-27 11:07:36.080  INFO 23467 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  2014-06-27 11:08:49.583  INFO 23467 --- [ost-startStop-1] o.s.boot.SpringApplication               : Started application in 183.152 seconds (JVM running for 210.258)
  2014-06-27 11:12:29.229 ERROR 23467 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/abcd]        : Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  
  java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4937)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
      at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
      at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
      at java.util.concurrent.FutureTask.run(FutureTask.java:166)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>There is no web.xml as i mentioned earlier.</p>
  
  <p>Few interesting things that i can't figure out why :</p>
  
  <ol>
  <li>After exploding the war, tomcat somehow create a ROOT folder with default web.xml[Must be Spring boot misconfiguration. How can i correct it? Pointers please?]</li>
  <li>Even if i return same 'application' SpringApplicationBuilder in AbcdXml.java, i am facing the same issue of applicationcontext being loaded twice.</li>
  </ol>
  
  <p>Thanks for your help!</p>
  
  <p>EDIT 1:</p>
  
  <p>Content of web.xml that is generated in ROOT folder :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"&gt;
  &lt;/web-app&gt;
  </code></pre>