title: Working with multiple dispatcher servlets in a spring application
body: <p>In my spring application, I have the following configuration classes for the spring environment:</p>
  
  <p><strong>WebAppInitializer.java</strong></p>
  
  <pre><code>@Order(value=1)
  public class WebAppInitializer implements WebApplicationInitializer {
  
      @SuppressWarnings("resource")
      @Override
      public void onStartup(ServletContext container) {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(WebAppConfig.class);
  
        // Manage the lifecycle of the root application context
        //container.addListener(new ContextLoaderListener(rootContext));
  
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(DispatcherConfig.class);
  
        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
      }
  
  }
  </code></pre>
  
  <p><strong>WebAppConfig.java</strong></p>
  
  <pre><code>@EnableWebMvc
  @EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
  @ComponentScan(value="spring.webapp.lojavirtual")
  @Configuration
  public class WebAppConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926);
          registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926);
          registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926);
      }
  
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  
  }
  </code></pre>
  
  <p><strong>DispatcherConfig.java</strong></p>
  
  <pre><code>@Controller
  @Import(WebAppConfig.class)
  public class DispatcherConfig {
  
      @Bean
      public ViewResolver jspResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setPrefix("/WEB-INF/jsp/");
          viewResolver.setSuffix(".jsp");
          return viewResolver;
      }
  
  }
  </code></pre>
  
  <p>I want add others dispatcher servlet to my application. My first idea was ass the following code to the classes above:</p>
  
  <p><strong>In WebAppInitializer.java</strong></p>
  
  <p>A new block like this, changing the names in the proper places:</p>
  
  <pre><code>// Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(DispatcherConfig.class);
  
        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
  </code></pre>
  
  <p>And add a new class like DispatcherConfig.java, with the name chosen in the code above.</p>
  
  <p>My questions are:</p>
  
  <p>1) First of all, my approach is the right way to add a new dispatcher servlet?</p>
  
  <p>2) Second, if the answer for question 1 is 'yes', which names I should change in the WebAppInitializer?</p>
  
  <p>3) In my controller(s), how I sinalize for which dispatcher servlet my requisition should go? My controllers use methods like the following for call a view:</p>
  
  <pre><code>@RequestMapping(value="view_mapping")
  public method() {
      ModelAndView mav = new ModelAndView()
      mav.setViewName("view_name");
      return mav;
  }
  </code></pre>