title: How to migrate existing Spring mvc app with WebApplicationInitializer -> Spring-boot-web-starter using "bottom up" approach without @EnableAutoConfig
body: <p>So I am trying to migrate a spring mvc (sp4) application to spring-boot application but I want to take a "bottom up" approach since I already have my configuration files made/tested and I know they work I just want to convert to a barebones spring-boot-web application where my existing configs will work on an embedded tomcat server. </p>
  
  <p>All the solutions I have read so far in the documentation take a "top down" approach where they suggest to import all the starter jars you need and <code>@EnableAutoConfiguration</code> and "turn off" configs you don't need over time. I think this "top down" approach works great if you're starting from scratch but not if you're migrating an existing application. </p>
  
  <p><strong>Problem:</strong> That being said i'm trying to migrate my existing sp4 mvc app via a "bottom up" approach without <code>@EnableAutoConfiguration</code>... but i'm having trouble creating a barebones spring-boot-web application with my existing configs.  </p>
  
  <p><strong>My configuration:</strong></p>
  
  <p>that implements a WebApplicationInitializer and @Override(s) the startup method  </p>
  
  <pre><code>public class FooWebAppWebApplicationInitializer implements WebApplicationInitializer {
  
      public static final String SERVLET_NAME = "foo-web-app";
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          // Create the 'root' Spring application context
          AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(RootConfig.class);
  
          // Manage the lifecycle of the root application context
          servletContext.addListener(new ContextLoaderListener(rootContext));
  
          // Create the dispatcher servlet's Spring application context
          AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
          dispatcherContext.register(SpringMvcConfig.class);
  
          // Register and map the dispatcher servlet
          ServletRegistration.Dynamic dispatcher = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(dispatcherContext));
  
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
  
          //Spring security config
          FilterRegistration.Dynamic springSecurityFilterChain = 
                  servletContext.addFilter(
                          "securityFilter", new DelegatingFilterProxy("springSecurityFilterChain")
                          );
  
          springSecurityFilterChain.addMappingForServletNames(null, false, SERVLET_NAME);
  
          servletContext.addFilter("hiddenHttpMethodFilter", HiddenHttpMethodFilter.class);
  
      }
  </code></pre>
  
  <p><strong>Note:</strong> With <code>SpringMvcConfig.class</code> contains an <code>@EnableWebMvc</code> annotation. </p>
  
  <p><strong>Note:</strong> no <code>@Component</code> scanning is used... all beans are <strong>EXPLICITLY</strong> declared in config classes. </p>
  
  <p><strong>Spring Boot Runner:</strong></p>
  
  <pre><code>@Configuration
  public class SpringBootRunner {
  
      public static void main(String[] args) {
  
          //SpringApplicationBuilder sab = new SpringApplicationBuilder();
          SpringApplication springApplication 
                      = new SpringApplication(RootConfig.class,
                                              SpringMvcConfig.class);
  
          springApplication.run(args);
          //SpringApplication.(SpringBootRunner.class, args);
      }
  
      @Bean
      public EmbeddedServletContainerFactory servletContainer() {
          TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
          factory.setPort(9000);
          factory.setSessionTimeout(10, TimeUnit.MINUTES);
          //factory.setErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
          return factory;
      }
  
  }
  </code></pre>
  
  <p><strong>Maven pom:</strong></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.3.1.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;groupId&gt;com.foo&lt;/groupId&gt;
      &lt;artifactId&gt;foo-web-app&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
          &lt;java-version&gt;1.8&lt;/java-version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
  
         //... omitted application specific dependencies 
         &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&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-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-amqp&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
          &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;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
           &lt;/plugins&gt;
  &lt;/build&gt;
  
  ...
  </code></pre>
  
  <hr>
  
  <p><strong>Things i've tried...</strong> </p>
  
  <ol>
  <li><p>As the spring boot documentation states: Convert the existing <code>WebApplicationInitializer</code> -> <code>SpringBootServletInitializer</code> and copy/paste the contents</p>
  
  <ul>
  <li><p>tried passing the SpringBootServletInitializer to my runner (Result: server starts but my config was ignored) </p></li>
  <li><p>tried <code>@Override</code> the configure method explicitly passing in the root / servlet context </p></li>
  </ul></li>
  </ol>
  
  <p>.</p>
  
  <pre><code>@Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      application.sources(RootConfig.class);
      application.child(SpringMvcConfig.class);
      return application.sources(SpringBootServletInitializer.class);
  }
  </code></pre>
  
  <p>that again resulted in the tomcat server starting but my config being ignored.</p>
  
  <ol start="2">
  <li>Tried to pass the root and mvc configs directly to the SpringBootRunner resulted in... the configs loading in the logger but threw the following exception</li>
  </ol>
  
  <p>.</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
      at org.springframework.util.Assert.notNull(Assert.java:115) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
  </code></pre>
  
  <hr>
  
  <p><strong>Question:</strong> Tried various other approaches still with no luck. Tired of guessing. I really couldn't find an example of what I am trying to achieve anywhere. Does anyone know of a sample project or know what runner/config I need to achieve my desired "bottom up" approach for a migration to spring boot? </p>
  
  <p>I just want to migrate my existing sp4 mvc config to a barebones spring-boot-web app and enable boot starters <strong>1 by 1</strong> after running my existing unit/integration tests on each starter jar I am migrating to.  </p>