title: applicationContext not finding Controllers for Servlet context
body: <p>I have a Spring web app with an applicationContext.xml and a dispatcher-servlet.xml configuration.  I've defined the <code>&lt;context:component-scan /&gt;</code> in applicationContext.xml, but when I run my app the Controllers are not found unless I also add <code>&lt;context:component-scan /&gt;</code> to the dispatcher-servlet.xml.  I'm using the same base-package in both, so that's not the issue.</p>
  
  <p>I am confused, because I <em>thought</em> that the applicationContext.xml was a parent of dispatcher-servlet.xml.  Wouldn't putting <code>&lt;context:component-scan /&gt;</code> in applicationContext.xml suffice?</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>EDIT: I am also using mvc:annotation-driven in the dispatcher-servlet.xml, which is supposed to pick up Controllers (I thought?).</p>
  
  <p>EDIT 2: Here are the config files.  I removed a bunch of Spring Security and OAuth settings from applicationContext.xml (for security reasons and being they probably aren't relevant anyway).</p>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
  &lt;context:component-scan base-package="bar.foo"/&gt;
  &lt;context:property-placeholder location="classpath:my.properties" /&gt;
  &lt;bean class="bar.foo.ServicesConfig" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>dispatcher-servlet.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
  &lt;context:component-scan base-package="bar.foo.controller" /&gt;
  &lt;mvc:annotation-driven/&gt;
  &lt;mvc:default-servlet-handler /&gt;
  
  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
      &lt;property name="order" value="2" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
      &lt;property name="mediaTypes"&gt;
          &lt;map&gt;
              &lt;entry key="json" value="application/json" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
      &lt;property name="defaultViews"&gt;
          &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt;
      &lt;/property&gt;
      &lt;property name="order" value="1" /&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>EDIT 3: Okay, this is interesting.  My services and dao classes are in a different project (JAR) that I reference from the web project.  I am using java-based config and referencing it from the applicationContext.xml:</p>
  
  <pre><code>&lt;bean class="bar.foo.config.ServicesConfig" /&gt;
  </code></pre>
  
  <p>So, this means there are only Controller annotations in my web project (where applicationContext.xml is located).  In retrospect, removing context:component-scan from my applicationContext.xml should not have any affect, since there are no annotations except for @Controller ones (FIX to EDIT: there are some @Autowired annotations).  But, when I remove the context:component-scan from applicationContext.xml, it says that the Controllers (found from dispatcher servlet scan) cannot find my Service classes.  Shouldn't the reference to the ServicesConfig be enough?  Here is the ServicesConfig class for references - it has its own component scan for the Services, which are a different package from what the applicationContext.xml was scanning.</p>
  
  <pre><code>@Configuration
  @ComponentScan({ "some.other.package", "another.package" })
  @ImportResource({ "classpath:commonBeans.xml" })
  @PropertySource({ "classpath:services.properties",
  "classpath:misc.properties" })
  public class ServicesConfig {
    // Bean definitions //
  }
  </code></pre>
  
  <p>SOLUTION:</p>
  
  <p>When I removed context:component-scan from my root context, the Controllers were not picking up the autowired services beans.  This was because the root context references my services java-based config Bean, but I did not have the root context setup to scan for Components.  Hence, when I add component scanning to the root context (applicationContext.xml) everything works.  Here is what I have now:</p>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;bean class="bar.foo.config.ServicesConfig" /&gt;
  &lt;context:component-scan base-package="bar.foo.config" /&gt;
  </code></pre>
  
  <p>dispatcher-servlet.xml:</p>
  
  <pre><code>&lt;context:component-scan base-package="bar.foo.controller" /&gt;
  </code></pre>
  
  <p>I have the web context setup to pickup Controller, Autowired, and any other annotations in the controller package - I'm not sure if this is best practice or not.</p>