title: How can I get a bean from the servlet context instead of the root context?
body: <p>I have a spring-mvc application which has autowired beans by use of <code>&lt;context:component-scan base-package="com.example.app" /&gt;</code> within the <code>DispatcherServlet</code> context configuration.</p>
  
  <p>I now have an situation where I wish to access a service bean from a non bean class, specifically a <code>RequestContextAwareTag</code> implementation.</p>
  
  <p>I can access beans registered in the root context as follows:</p>
  
  <pre><code>ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(
        pageContext.getSession().getServletContext());
  MyService svc = ctx.getBean(MyService.class);
  </code></pre>
  
  <p>If the bean is registered in the dispatcher context I get a <code>NoSuchBeanDefinitionException</code>.</p>
  
  <p>If it's possible I would actually prefer my <code>@Service</code> beans to be registered in the root context without the <code>@Controller</code> beans being picked up, and then have the <code>@Controller</code> beans picked up in the dispatcher context. The problem with <code>&lt;context:component-scan/&gt;</code> is it picks up both.</p>
  
  <p>If this is not possible I need a way to access the dispatchers <code>ApplicationContext</code> to retrieve the service beans.</p>
  
  <p>Any guidance would be very much appreciated.</p>