title: Spring DI - Autowired property is null in a REST service
body: <p>I'm getting started with Spring DI, but I'm struggling with dependency injection and the worse part is that I'm not even sure why as it seems ok to me. Hopefully you guys can help me out! </p>
  
  <p><strong>The problem is that a property annotated as <em>@Autowired</em> is always <em>null</em></strong></p>
  
  <p>I've got a few projects with Maven structure:</p>
  
  <ul>
  <li>com.diegotutor.lessondeliver</li>
  <li>com.diegotutor.utility</li>
  </ul>
  
  <p>I'm running the examples over Tomcat 7</p>
  
  <p>I'm using the following dependencies in my <code>pom.xml</code>:</p>
  
  <ul>
  <li>spring-context 3.2.4</li>
  <li>spring-web 3.2.4</li>
  <li>jersey-server 1.17.1</li>
  <li>jersey-core 1.17.1</li>
  <li>jersey-servlet 1.17.1</li>
  </ul>
  
  <p>The simple idea is to have a RESTful service that through Dependency Injection is able to print out the value of a property located in a config file located at: <code>D:\configuracion.conf.</code></p>
  
  <p>At <strong>com.diegotutor.utility</strong> I have the following interface:</p>
  
  <pre><code>package com.diegotutor.utility;
  
  public interface ConfigService {
  
      public String getProperty(final String propertyName);
  }
  </code></pre>
  
  <p>Implemented by:</p>
  
  <pre><code>package com.diegotutor.utility.impl;
  
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.Reader;
  import java.util.Properties;
  
  import com.diegotutor.utility.ConfigService;
  
  public class PropertyFileConfigService implements ConfigService{
  
  Properties prop;
  
  public PropertyFileConfigService (final InputStream input) throws IOException {
      if(input == null) {
          throw new IllegalArgumentException("Input stream can't be null");
      }
      prop = new Properties();
      prop.load(input);
  }
  
  public PropertyFileConfigService (final String fileName) throws IOException {
      final FileInputStream input = new FileInputStream(fileName);
      prop = new Properties();
      prop.load(input);
  }
  
  public PropertyFileConfigService(final Reader input) throws IOException {
      prop = new Properties();
      prop.load(input);
  }
  
  public String getProperty(final String propertyName) {
      return prop.getProperty(propertyName);
  }
  
  }
  </code></pre>
  
  <p>And at <strong>com.diegotutor.lessondeliver</strong> I have the RESTful service where I would like to use an injected instance of the ConfigService:</p>
  
  <pre><code>package com.diegotutor.lessondeliver;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import com.diegotutor.utility.ConfigService;
  
  @Path("/")
  @Component
  public class HelloWorld {
  
  private static final Log log = LogFactory.getLog(HelloWorld.class);
  
  @Autowired
  private ConfigService configService;
  
  @Path("/helloworld")
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String getHello() {
      String host = configService.getProperty("host");
      return "Hello World! HOST" + host;
              // configService IS NULL!! 
              //SO IT THROWS A NULLPOINTER EXCEPTION WHEN INVOKING getProperty ON IT
  }
  }
  </code></pre>
  
  <p>Finally at <code>/com.diegotutor.lessondeliver/src/main/webapp/WEB-INF/service-beans.xml</code> I have the following XML application context file, where I use the implementation of <code>ConfigService (PropertyFileConfigService)</code> injecting on it the path for the configuration file to read:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;bean id="configService" class="com.diegotutor.utility.impl.PropertyFileConfigService"&gt;
      &lt;constructor-arg type="java.lang.String"
          value="D:\configuracion.conf" /&gt;
  &lt;/bean&gt;
  
  &lt;context:component-scan base-package="com.diegotutor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><em>Obviously I have specified in the <code>web.xml</code> of this <code>com.diegotutor.lessondeliver</code> web app that I want <code>service-beans.xml</code> as ConfigLocation and a listener <code>ContextLoaderListener</code>, and the RESTful service relies on ServletContainer</em></p>
  
  <p>If I'm specifying context:component-scan to look for Components in <code>com.diegotutor</code> as suggested <a href="https://stackoverflow.com/questions/17589757/problems-with-autowire-annotation-null">here</a> and I'm forcing object creation through Spring by not using any new Statement as suggested <a href="https://stackoverflow.com/questions/18091939/autowired-field-get-null">here</a>, <strong>Why am I getting the annotated configService as null? Why Spring is unable to inject an instance of <code>com.diegotutor.utility.impl.PropertyFileConfigService</code>?</strong></p>
  
  <p>Any help will be much appreciated!</p>
  
  <p>Thank you</p>
  
  <p>EDITED:</p>
  
  <p>As requested, my web.xml is as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
  id="WebApp_ID" version="3.0"&gt;
  
   &lt;display-name&gt;com.diegotutor.lessondeliver&lt;/display-name&gt;
  
   &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/service-beans.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;jersey-servlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;
           com.sun.jersey.spi.container.servlet.ServletContainer
    &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;jersey-servlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
   &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>