title: Spring @Autowired not working (not always)
body: <p>I have some problems with autowire annotation in one of my services. I have spent a lot of hours to find a solution but I don't have idea what I'm doing wrong.  My app looks like this.</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>package control.peso.controller;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.ResponseBody;
  
  import control.peso.data.ResumenMedicionPeso;
  import control.peso.service.HomeService;
  
  @Controller
  public class HomeController {
  
      @Autowired
      private HomeService homeService; //NOPMD
  
      @RequestMapping(value = "json/resumen_mediciones.action")
      @ResponseBody
      public final ResumenMedicionPeso
              dataJsonPeso(final HttpServletRequest req) {
          final ResumenMedicionPeso peso = homeService.getResumenMediciones();
  
          return peso;
      }
  }
  </code></pre>
  
  <p>My service layer:</p>
  
  <pre><code>package control.peso.service;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import control.peso.dao.PesoDAO;
  import control.peso.data.ResumenMedicionPeso;
  
  @Service
  @Transactional(readOnly = true)
  public class HomeService {
  
      @Autowired
      private PesoDAO pesoDAO; //NOPMD
  
      public final ResumenMedicionPeso getResumenMediciones() {
          final ResumenMedicionPeso resumMedicionPeso = new ResumenMedicionPeso();
          resumMedicionPeso.setMaxPeso(pesoDAO.getMaxPeso());
          resumMedicionPeso.setMinPeso(pesoDAO.getMinPeso());
          resumMedicionPeso.setMaxGrasa(pesoDAO.getMaxGrasa());
          resumMedicionPeso.setMinGrasa(pesoDAO.getMinGrasa());
          resumMedicionPeso.setMaxPorcenGrasa(pesoDAO.getMaxPorcenGrasa());
          resumMedicionPeso.setMinPorcenGrasa(pesoDAO.getMinPorcenGrasa());
          resumMedicionPeso.setMaxMusculo(pesoDAO.getMaxMusculo());
          resumMedicionPeso.setMinMusculo(pesoDAO.getMinMusculo());
          resumMedicionPeso.setMaxPorcenMusculo(pesoDAO.getMaxPorcenMusculo());
          resumMedicionPeso.setMinPorcenMusculo(pesoDAO.getMinPorcenMusculo());
  
          return resumMedicionPeso;
      }
  }
  </code></pre>
  
  <p>My dao:</p>
  
  <pre><code>package control.peso.dao;
  
  import java.util.List;
  
  import org.hibernate.SessionFactory;
  
  import control.peso.model.MedicionPeso;
  
  public class PesoDAO implements IPesoDAO {
  
      private SessionFactory sessionFactory;
  
      public final SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      public final void setSessionFactory(
              final SessionFactory pSessionFactory) {
          this.sessionFactory = pSessionFactory;
      }
  
      @Override
      public final void addPeso(final MedicionPeso peso) {
          getSessionFactory().getCurrentSession().save(peso); //NOPMD
      }
  
      @Override
      public final void updatePeso(final MedicionPeso peso) {
          getSessionFactory().getCurrentSession().update(peso);  //NOPMD
      }
  
      @Override
      public final void deletePeso(final Integer idPeso) {
          getSessionFactory().getCurrentSession()
          .delete(new MedicionPeso(idPeso));
      }
  
      @Override
      public final MedicionPeso getPesoById(final Integer idPeso) {
          @SuppressWarnings("unchecked") //NOPMD
          final List&lt;MedicionPeso&gt; list = getSessionFactory() // NOPMD
                  .getCurrentSession()
                          .createQuery("from MedicionPeso where idPeso = ?")
                  .setParameter(0, idPeso).list();
  
          return list.get(0); //NOPMD
      }
  
      @Override
      public final List&lt;MedicionPeso&gt; getPesos() {
          @SuppressWarnings("unchecked")
          final List&lt;MedicionPeso&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("from MedicionPeso medicionPeso "
                          + "order by medicionPeso.fechaMedicion desc")
                  .list();
  
          return list;
      }
  
      public final Float getMaxPeso() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(peso) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMinPeso() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select min(peso) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMaxGrasa() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(pesoGrasa) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMinGrasa() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select min(pesoGrasa) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMaxPorcenGrasa() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(pesoGrasa) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMinPorcenGrasa() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select min(porcentajeGrasa) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      /**
       * Recupera la medicion de musculo con valor maximo.
       * @return El valor maximo de las mediciones de musculo.
       */
      public final Float getMaxMusculo() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(porcentajeGrasa) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMinMusculo() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select min(pesoMusculo) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMaxPorcenMusculo() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(porcentajeMusculo) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
      public final Float getMinPorcenMusculo() {
          @SuppressWarnings("unchecked")
          final List&lt;Float&gt; list = getSessionFactory() //NOPMD
                  .getCurrentSession()
                  .createQuery("select max(porcentajeMusculo) from MedicionPeso")
                  .list();
  
          return (Float) list.get(0); //NOPMD
      }
  
  }
  </code></pre>
  
  <p>My dao interface:</p>
  
  <pre><code>package control.peso.dao;
  
  import java.util.List;
  
  import control.peso.model.MedicionPeso;
  
  public interface IPesoDAO {
  
      void addPeso(MedicionPeso peso);
  
      void updatePeso(MedicionPeso peso);
  
      void deletePeso(Integer idPeso);
  
      MedicionPeso getPesoById(Integer idPeso);
  
      List&lt;MedicionPeso&gt; getPesos();
  }
  </code></pre>
  
  <p>This is my dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
      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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
      &lt;!--Routes --&gt;
      &lt;mvc:view-controller path="/" view-name="home"/&gt;
      &lt;mvc:view-controller path="/home" view-name="home"/&gt;
      &lt;mvc:view-controller path="/medicion" view-name="medicion_peso"/&gt;
  
      &lt;!-- Scans the classpath of this application for @Components to deploy as beans --&gt;
      &lt;context:component-scan base-package="control.peso" /&gt;
  
      &lt;!-- Configures the @Controller programming model --&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;!-- misc --&gt;
      &lt;!--
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
      --&gt;
      &lt;!-- Tiles Resolver --&gt;
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"&gt;
              &lt;value&gt;
                  org.springframework.web.servlet.view.tiles2.TilesView
              &lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
          &lt;property name="definitions"&gt;
              &lt;list&gt;
                  &lt;value&gt;/WEB-INF/tiles.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Application Message Bundle --&gt;
      &lt;bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- JSON Objets Definition --&gt;
      &lt;bean
          class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Beans Declaration --&gt;
      &lt;bean id="MedicionPeso" class="control.peso.model.MedicionPeso" /&gt;
  
      &lt;!-- User DAO Declaration --&gt;
      &lt;bean id="PesoDAO" class="control.peso.dao.PesoDAO"&gt;
          &lt;property name="sessionFactory" ref="SessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Data Source Declaration --&gt;
      &lt;bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/juan" /&gt;
          &lt;property name="user" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
          &lt;property name="maxPoolSize" value="10" /&gt;
          &lt;property name="maxStatements" value="0" /&gt;
          &lt;property name="minPoolSize" value="5" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Session Factory Declaration --&gt;
      &lt;bean id="SessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="DataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;control.peso.model.MedicionPeso&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Enable the configuration of transactional behavior based on annotations --&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
      &lt;!-- Transaction Manager is defined --&gt;
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="SessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>So when I launch my web-app, my controller Autowires correctly the service, but my DAO object in my service has null value (not injected properly).</p>
  
  <p>Any ideas?</p>
  
  <p>It's curious because from another service, the same DAO is properly injected.</p>
  
  <p>Thanks</p>