title: Use @Autowire in servlet
body: <p>I want to use <code>@Autowired</code> in my servlet to bring in some external configurations.
  Here is my servlet</p>
  
  <pre><code>public class DashboardServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private final String USER_AGENT = "Mozilla/5.0";
  
  private String backendUserPassword="";
  public DashboardServlet() {
      super();
  }
  
  private WebApplicationContext springContext;
  
  @Autowired
  BackendHostConfiguration backendHostConfiguration;
  
  
  
  public void init(ServletConfig config) throws ServletException {
  
      super.init(config);
      springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
      final AutowireCapableBeanFactory beanFactory = springContext.getAutowireCapableBeanFactory();
      beanFactory.autowireBean(this);
      backendUserPassword = backendHostConfiguration.getUserpassword();
  }
  
  
  
  protected void doGet...
  </code></pre>
  
  <p>However, my backenHostConfiguration is always null.  Can someone help me?</p>
  
  <p>Here is my web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
  &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;description&gt;&lt;/description&gt;
      &lt;display-name&gt;DashboardServlet&lt;/display-name&gt;
      &lt;servlet-name&gt;DashboardServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.apple.store.unifiedproj.proxyapi.DashboardServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;DashboardServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/dashboard/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>