title: @Autowired is not working with jersey and spring
body: <p><strong>When I am running test at that time @Autowired is working but when I run the web app and try to fetch data at that time its throwing null pointer exception.</strong></p>
  
  <p>this is my controller</p>
  
  <p><strong>In this BuyerRepo is always null</strong></p>
  
  <pre><code>import com.retail.exception.InvalidIdException;
  import com.retail.model.Buyer;
  import com.retail.repository.BuyerRepo;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  
  @Path("/buyer")
  @Component
  public class BuyerController {
  
      @Autowired
      private BuyerRepo buyerRepo;
  
      @GET
      @Produces("application/json")
      public Buyer searchFields() throws InvalidIdException {
          String buyerId = "51";
          Buyer buyer;
  
          try {
              buyer = buyerRepo.getBuyer(Long.parseLong(buyerId));
          } catch (NumberFormatException e) {
              buyer = buyerRepo.getBuyer(buyerId);
          }
  
          return buyer;
      }
  }
  </code></pre>
  
  <p><strong>In repository entity manager is always null</strong></p>
  
  <p>this is buyerRepository</p>
  
  <pre><code>import com.retail.exception.InvalidIdException;
  import com.retail.model.Buyer;
  import org.springframework.stereotype.Repository;
  
  import javax.persistence.NoResultException;
  
  @Repository
  public class BuyerRepo extends AbstractRepository {
  
      public Buyer getBuyer(String buyerName) throws InvalidIdException {
          javax.persistence.Query buyerId = entityManager.createNativeQuery("select b.buyer_id from buyer b where b.name = :name").setParameter("name", buyerName);
          Integer id;
  
          try {
              id = (Integer) buyerId.getSingleResult();
  
          } catch (NoResultException e) {
              return null;
          }
  
          return getBuyer(id);
      }
  
      public Buyer getBuyer(long buyerId) throws InvalidIdException {
          Buyer buyer = entityManager.find(Buyer.class, buyerId);
          if (buyer == null) throw new InvalidIdException("Invalid Article ID");
          return buyer;
      }
  }
  </code></pre>
  
  <p>this is applicationContext.xml</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"
         xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="com.retail"/&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.postgresql.Driver"/&gt;
          &lt;property name="url" value="jdbc:postgresql://localhost/retail"/&gt;
          &lt;property name="username" value="retail_user"/&gt;
          &lt;property name="password" value="password"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerOne" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
  
          &lt;property name="packagesToScan" value="com.retail"/&gt;
  
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
                  &lt;property name="showSql" value="true"/&gt;
                  &lt;property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerOne"/&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;/beans&gt;
  </code></pre>
  
  <p>this is servlet-context.xml</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"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;mvc:annotation-driven /&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;context:component-scan base-package="com.retail" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>this is 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/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_5.xsd"&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;retail&lt;/servlet-name&gt;
  
          &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
              &lt;param-value&gt;com.retail.web&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt;
              &lt;param-value&gt;true&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;retail&lt;/servlet-name&gt;
  
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              classpath:applicationContext.xml
              /WEB-INF/servlet-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;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/WEB-INF/views/index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>