title: spring-boot with tomcat and cxf-servlet
body: <p>I'm trying to stand up an embedded Tomcat with spring-boot. I want to use CXF for a set of web services in the app but I can not figure out how to stand up the CXF servlet.</p>
  <p>My Main class looks like this...</p>
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan(basePackages={&quot;com.connecture.services.documentservice.webservice&quot;})
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(new Class[] { Application.class, CfxInitializer.class }, args);
      }
      
      @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(&quot;&quot;, 8080);
        return factory;
    }
  
  }
  </code></pre>
  <p>And my CfxInitializer like this...</p>
  <pre><code>public class CfxInitializer implements ServletContextInitializer
  {
  
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException
    {
      XmlWebApplicationContext rootContext = new XmlWebApplicationContext();  
      rootContext.setConfigLocations(new String[] { &quot;classpath*:applicationContext.xml&quot; });  
      servletContext.addListener(new ContextLoaderListener(rootContext));  
  
      ServletRegistration.Dynamic dispatcher = servletContext.addServlet(&quot;CXFServlet&quot;, CXFServlet.class);  
      dispatcher.addMapping(&quot;/api/*&quot;);  
    }
  
  }
  </code></pre>
  <p>When I try to build and start the jar with the typical command <code>./gradlew build &amp;&amp; java -jar build/libs/gs-spring-boot-0.1.0.jar</code></p>
  <p>I get an Exception for multiple Contexts.</p>
  <pre><code>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:4971)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  <p>Here is a more complete pastebin - <a href="http://pastebin.com/bcJ2ULhM" rel="nofollow noreferrer">http://pastebin.com/bcJ2ULhM</a></p>
  <h1>----------------------------------------------------------------------------------</h1>
  <p>Similarly to Dave's answer I was able to fix it by removing the ServletContextInitializer and adding a bean to the Application Class.</p>
  <pre><code>@Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new CXFServlet(),&quot;/api/*&quot;);
    }
  </code></pre>