title: Scan for components in services module of multi module maven spring project
body: <p>I have a Maven Multi Module project with the following struture:</p>
  
  <ul>
  <li>Site-Parent<br/>
  -- Site-UI<br/>
  -- Site-Entities<br/>
  -- Site-Services<br/>
  -- Sites-Repository<br/></li>
  </ul>
  
  <p><br/>
  My Site-UI web.xml:</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.mysite.app.web.spring&lt;/param-value&gt;
  `&lt;/context-param&gt;
  </code></pre>
  
  <p><br/>
  AppConfig.java within com.mysite.app.web.spring:</p>
  
  <pre><code>@Configuration
  @ImportResource({ "classpath*:contextConfig.xml",
          "classpath*:securityConfig.xml" })
  @ComponentScan(basePackages = "com.mysite.app.web.controller")
  public class AppConfig
  {
      @Bean
      public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter()
      {
          ...
      }
  
      @Bean
      public SpringTemplateEngine templateEngine()
      {
          ...
      }
  
      ...
  }
  </code></pre>
  
  <p><br/>
  WebConfig.java within com.mysite.app.web.spring:</p>
  
  <pre><code>@EnableWebMvc
  @ComponentScan({ "com.mysite.app.web.controller" })
  @Configuration
  public class WebConfig extends WebMvcConfigurerAdapter
  {
  
      public WebConfig()
      {
          super();
      }
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry)
      {
          registry.addResourceHandler("/css/**").addResourceLocations("/css/");
          registry.addResourceHandler("/js/**").addResourceLocations("/js/");
          registry.addResourceHandler("/img/**").addResourceLocations("/img/");
      }
  
      @Override
      public void addInterceptors(InterceptorRegistry reg)
      {
          reg.addInterceptor(new BaseInterceptor());
          reg.addInterceptor(new SecurityInterceptor());
          //reg.addInterceptor(new AddCardInterceptor());
      }
  }
  </code></pre>
  
  <p><br/>
  Here is one of my controllers:</p>
  
  <pre><code>@Controller
  public class PurchaseController extends AppController
  {
      static Logger logger = LoggerFactory.getLogger(PurchaseController.class);
  
      @Autowired
      private IPurchaseService purchaseService;
  
          ...
  }
  </code></pre>
  
  <p><br/>
  I get the following error when starting up the app:</p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'purchaseController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mysite.services.IPurchaseService com.mysite.app.web.controller.PurchaseController.purchaseService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
      at java.util.concurrent.FutureTask.run(FutureTask.java:138)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mysite.services.IPurchaseService com.mysite.app.web.controller.PurchaseController.purchaseService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
      ... 23 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mysite.services.IPurchaseService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
      ... 25 more
  </code></pre>
  
  <p>I know it is because my services are not been scanned.</p>
  
  <p>I have the following in my services module:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "com.mysite.services")
  public class ServicesConfig
  {
  }
  </code></pre>
  
  <p>What I need to know is how can I configure my app to scan them? 
  I want to maintain as much configuration in code as possible and avoid using xml files.</p>
  
  <p>Thanks</p>