title: Spring Java Config with Multiple Dispatchers
body: <p>I've some experience Spring now and also have some pure java config web-apps in use. However, these are usually based on a quiet simple setup:</p>
  
  <ul>
  <li>application config for services / repositories</li>
  <li>dispatcher config for one dispatcher (and some controllers)</li>
  <li>(optional) spring security to secure the access</li>
  </ul>
  
  <p>For my current project I need to have separate dispatcher contexts with different configuration. That's not a problem with the XML based configuration as we have a dedicated ContextLoaderListener that's independent from Dispatcher Configuration. But with java config I'm not sure if what I'm doing is fine so far ;)</p>
  
  <p>Here's a common DispatcherConfig:</p>
  
  <pre><code>public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
    @Override
    protected Class&lt;?&gt;[] getRootConfigClasses() {
      return new class[]{MyAppConfig.class};
    }
  
    @Override
    protected Class&lt;?&gt;[] getServletConfigClasses() {
      return new Class[]{MyDispatcherConfig.class};
    }
  
    @Override
    protected String[] getServletMappings() {
      return new String[]{"/mymapping/*"};
    }
  
    @Override
    protected String getServletName() {
      return "myservlet";
    }
  }
  </code></pre>
  
  <p>As said, I need a second (third, ...) dispatcher with another mapping (and view resolvers). So, I copied the config and added for both getServletName() (otherwise both will be named as 'dispatcher' which will cause errors). The second config was looking like that:</p>
  
  <pre><code>public class AnotherWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
    @Override
    protected Class&lt;?&gt;[] getRootConfigClasses() {
      return new class[]{MyAppConfig.class};
    }
  
    @Override
    protected Class&lt;?&gt;[] getServletConfigClasses() {
      return new Class[]{AnotherDispatcherConfig.class};
    }
  
    @Override
    protected String[] getServletMappings() {
      return new String[]{"/another_mapping/*"};
    }
  
    @Override
    protected String getServletName() {
      return "anotherservlet";
    }
  }
  </code></pre>
  
  <p>When I use it like this, starting application results in a problem with ContextLoaderListener:</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)
  ...
  </code></pre>
  
  <p>So I removed the second <strong>MyAppConfig.class</strong> return from one of the 
  <em>AbstractAnnotationConfigDispatcherServletInitializer</em> and it works fine. However, that doesn't feel to be the right way ;)</p>
  
  <p>For my understanding: should all DispatcherConfig be handled within one <em>AbstractAnnotationConfigDispatcherServletInitializer</em> or should I separate them as I did? I tried to configure them in one class but then my config was totally mixed (so I believe that's not the desired way).</p>
  
  <p>How do you implement such a case? Is it possible to set the <em>ContextLoaderListener</em> in java config outside of the <em>AbstractAnnotationConfigDispatcherServletInitializer</em>? Or should I create a <em>DefaultServlet</em> which has only the root config? What about implementing the base interface of that configuration <em>WebApplicationInitializer</em>?</p>