title: Cannot inject beans in servlet context to Controller
body: <p>I have read <a href="https://stackoverflow.com/questions/25807872/injecting-jaxb2marshaller-in-spring-mvc-controller">Injecting Jaxb2Marshaller in Spring MVC controller</a> and <a href="https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring">Difference between applicationContext.xml and spring-servlet.xml in Spring Framework</a> etc but feeling confused on why my case is not working.</p>
  
  <p>Here is what I have in my app contexts and web.xml  (trimmed down to show only related parts):</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app .... &gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:/app-context.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;spring-rest&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring-rest&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      ...
  &lt;/web-app&gt;
  </code></pre>
  
  <p>spring-rest-servlet.xml</p>
  
  <pre><code>&lt;beans ....&gt;
  
      &lt;!-- Only scan for Controllers in Servlet context --&gt;
      &lt;context:component-scan base-package="com.fil.ims" use-default-filters="false"&gt;
          &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt;
          &lt;context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/&gt;
      &lt;/context:component-scan&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"&gt;
           ....
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I have a controller something like this:</p>
  
  <pre><code>@Controller
  public class FooControllerImpl implements FooController {
      @Inject
      @Named("jaxb2Marshaller")
      Jaxb2Marshaller jaxb2Marshaller;
  
      public void setJaxb2Marshaller(Jaxb2Marshaller jaxb2Marshaller) {
          this.jaxb2Marshaller = jaxb2Marshaller;
      }
      //.....
  }
  </code></pre>
  
  <p>I found that I cannot inject the jaxb2Marshaller in the servlet context (<code>NoSuchBeanDefinitionException</code> is thrown).  However if I have the <code>jaxb2Marshaller</code> put in the main app-context (<code>app-context.xml</code>), it can be injected normally.</p>
  
  <p>I am confused why it happens.  From my understanding, if I have <code>ContextLoaderListener</code> in <code>web.xml</code>, the provided context config  (app-context.xml) will be used to create a top-level app context, and then the dispatcher servlet context (<code>spring-rest-servlet.xml</code> in my example) will be created as a child context under the top-level app context.  </p>
  
  <p>What I don't understand is, both the controller and the bean to be injected (jaxb2Marshaller) is all created under the dispatcher servlet context, why I cannot inject <code>jaxb2Marshaller</code> into my <code>FooController</code>?  (I understand it will not work if the bean to be injected is located in child app context, but in this case it seems not.)</p>
  
  <p>Can someone explain why?</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>I have done another experiment (which makes me even more confusing), by implementing <code>ApplicationContextAware</code> in my controller, and from the controller, I do <code>applicationContext.getBean("jaxb2Marshaller", Jaxb2Marshaller.class)</code>.  A valid bean is returned (which is aligned with my understanding).  It seems only injection through annotation is not working.  Any clues?</p>