title: Right way to use @ComponentScan in multi module Java-Config Spring MVC app
body: <p>I've just started a new spring project, and this time I want to do things "right". In the last project I had issues with multiple registering of certain classes because of multiple <code>@ComponentScan</code> annotations. (i.e. all service classes got registered twice)</p>
  
  <p>Basically I'm using the following layout:</p>
  
  <p><code>WebAppInitializer</code>:</p>
  
  <pre><code>public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] { RootConfig.class };
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class[] { WebMvcConfig.class };
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[] { "/" };
      }
  
  }
  </code></pre>
  
  <p><code>RootConfig</code>:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  public class RootConfig {
      /* ... */
  }
  </code></pre>
  
  <p><code>WebMvcConfig</code>:</p>
  
  <pre><code>@EnableWebMvc
  @ComponentScan
  public class WebMvcConfig extends WebMvcConfigurerAdapter {
      /* ... */
  }
  </code></pre>
  
  <p><code>DatabaseConfig</code>:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories("my.base.class.path")
  public class DataConfig {
      /* ... */
  }
  </code></pre>
  
  <p><strong>The first basic question is:</strong> <em>Which class should scan for which classes/annotations?</em></p>
  
  <p>Should only the <code>WebMvcConfig</code> scan for <code>@Controller</code> classes? Which one should scan for <code>@Service</code> and for <code>@Configuration</code> and for <code>@Component</code>?</p>
  
  <p><strong>The second question is:</strong> <em>Or should I simply use packages to narrow down the scan path?</em></p>
  
  <p>Example:</p>
  
  <pre><code>rootpackage
  rootpackage.config.RootConfig
  rootpackage.config.DatabaseConfig
  rootpackage.mvc.WebMvcConfig
  </code></pre>
  
  <p>and then place all <code>@Controller</code> classes under <code>rootpackage.mvc.*</code>?</p>
  
  <p><strong>The third question is:</strong> <em>Is it more common to let <code>RootConfig</code> scan for <code>DatabaseConfig</code>? Or should I place <code>DatabaseConfig</code> inside the <code>getRootConfigClasses</code> method of <code>WebAppInitializer</code> class?</em></p>
  
  <p><strong>The last question is:</strong> <em>In a multi module project: How do you organize those things?</em></p>
  
  <p>Example: If I chose the way I described in <em>question two</em>, I could say, that every module of the app will in fact consist of a few different modules. Let's say, I want to create a module <code>X</code> which will have a <code>@Service</code> class and a few <code>@Controller</code> classes, I could put them in them in different packages. Like this:</p>
  
  <p><code>Maven Module X Service</code></p>
  
  <pre><code>rootpackage.services.x.XService
  rootpackage.services.x.XServiceImpl
  </code></pre>
  
  <p><code>Maven Module X Controller</code></p>
  
  <pre><code>rootpackage.mvc.controller.x.X1Controller
  rootpackage.mvc.controller.x.X2Controller
  rootpackage.mvc.controller.x.X3Controller
  </code></pre>
  
  <p>And if you'd suggest this way, then: Where to place models and repositories (for accessing the database)? Should I create a new module for each of those?</p>
  
  <p>Thanks in advance! </p>