title: Spring Boot with multiple DispatcherServlet, each having their own @Controllers
body: <p>Basically I want to split my application into 2 parts. Each part has it's own security stuff and own <code>@Controller</code>s. The <code>@Services</code> should be accessible from both parts.</p>
  
  <p>So I thought, I should get 2 <code>DispatcherServlet</code>. One listening to <code>/admin/*</code> and the second listening to everything else ( <code>/</code> ). Each of those will have its own <code>AnnotationConfigWebApplicationContext</code> so I can have separate component scan for the <code>@Controller</code>s.</p>
  
  <p>And because Spring Boot provides one <code>DispatcherServlet</code> listening on <code>/</code> out of the box, I thought, I can just add a second one:</p>
  
  <pre><code>@Configuration
  public class MyConfig {
      @Bean(name="myDS")
      public DispatcherServlet myDS(ApplicationContext applicationContext) {
          AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
          webContext.setParent(applicationContext);
          webContext.register(MyConfig2.class);
          // webContext.refresh();
          return new DispatcherServlet(webContext);
      }
  
      @Bean
      public ServletRegistrationBean mySRB(@Qualifier("myDS") DispatcherServlet dispatcherServlet) {
          ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet);
          servletRegistrationBean.addUrlMappings("/admin/*");
          servletRegistrationBean.setName("adminServlet");
          return servletRegistrationBean;
      }
  }
  </code></pre>
  
  <p>The <code>MyConfig2</code> class, only has <code>@Configuration</code> and <code>@ComponentScan</code>. Within the same package is a <code>@Controller</code>.</p>
  
  <p>When starting the application, I can see, that the second servlet mapping is getting registered, but the <code>@Controller</code> is not. Additionally I can now access <strong>all</strong> <code>@Controllers</code> from <code>/</code> <strong>and</strong> <code>/admin</code>.</p>
  
  <hr>
  
  <p>Any idea how I can get this working? </p>