title: SessionFactory not initialized
body: <p>i created a DAO (ForecastPersistorDao.java) which is the interface and its corresponding implementation (ForecastPersistorDaoImpl.java). The implementation has a method 'persist()' whose job is to persist some data into the database. When I try to call persist to persist the data, it throws a NullPointerException since it does not initialize SessionFactory properly. Please help me point out what I might be doing wrong:</p>
  
  <h2>ForecastPersistorDao.java</h2>
  
  <pre><code>public interface ForecastPersistorDao {
  
      void persist(List&lt;ForecastedDemand&gt; forecastedDemands);
      List&lt;DemandForecast&gt; retrieveLastForecast(String marketplaceId);
  
  }
  </code></pre>
  
  <h2>ForecastPersistorDaoImpl.java</h2>
  
  <pre><code>@Repository("forecastPersistorDao")
  public class ForecastPersistorDaoImpl implements ForecastPersistorDao {
  
      private SessionFactory sessionFactory;
  
      @Autowired
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      /**
       * Persist forecast in the database for later
       * 
       * @param forecastedDemands
       *            List of forecast for all asin:marketplaceId tuple
       */
      @Transactional
      @Override
      public void persist(List&lt;ForecastedDemand&gt; forecastedDemands) {
          System.out.println("THIS IS ALWAYS NULL--------&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; " + sessionFactory);
          Session session = sessionFactory.getCurrentSession();
          Date forecastCalculationDate = new Date();
  
          // For each [asin:marketplace] tuple
          for (ForecastedDemand forecastedDemand : forecastedDemands) {
              String asin = forecastedDemand.getAsinMarketplaceId().getAsin();
              String marketplaceId = forecastedDemand.getAsinMarketplaceId().getMarketplaceId();
              String forecastingModel = forecastedDemand.getForecastingModel();
              SortedMap&lt;Instant, Double&gt; forecast = forecastedDemand.getForecast();
  
              // for each forecast date - write an entry in demand_forecasts table
              for (Map.Entry&lt;Instant, Double&gt; entry : forecast.entrySet()) {
                  Date dateOfForecast = entry.getKey().toDate();
                  double quantityForecasted = entry.getValue();
                  DemandForecast forecastToPersist = new DemandForecast(asin, marketplaceId, forecastCalculationDate,
                          forecastingModel, quantityForecasted, dateOfForecast);
                  session.save(forecastToPersist);
              }
          }
      }
  </code></pre>
  
  <h2>Main class (Runner.java):</h2>
  
  <pre><code>public final class FbsRunner {
      private ForecastPersistorDao forecastPersistorDao;
  
      public static void main(String[] args) {
          Runner runner = new Runner();
          runner.run();
      }
  
      public void run() {
          ApplicationContext context =
                  new FileSystemXmlApplicationContext("spring-configuration/application-config.xml");
          forecastPersistorDao = (ForecastPersistorDao) context.getBean("forecastPersistorDao"); // this works fine
  
          System.out.println("&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; forecastPersistorDao [this is fine (not null)]: " + forecastPersistorDao);
  
          List&lt;ForecastedDemand&gt; forecastedDemand = [a list of Forecasted demand to be persisted int he DB]
          // THE CALL BELOW FAILS...
          forecastPersistorDao.persist(forecastedDemands);
          System.out.println("Persisted demand in the database"); // We don't reach here.
      }
  }
  </code></pre>
  
  <h2>spring-configuration/application-config.xml</h2>
  
  <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:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
      "&gt;
  
      &lt;!--  The main application context spring configuration --&gt;
      &lt;import resource="application/hibernate.xml" /&gt;
      &lt;import resource="common/hibernate.xml" /&gt;
  
      &lt;!--&lt;import resource="application/proxies.xml" /&gt;--&gt;
      &lt;!--&lt;import resource="common/aggregators.xml" /&gt; --&gt;
      &lt;import resource="application/environment.xml" /&gt;
      &lt;!--
          Add any beans specific to your application here
      --&gt;
      &lt;bean id="forecastPersistorDao" class="com.amazon.fresh.fbs.dao.ForecastPersistorDaoImpl" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <h2>application/hibernate.xml:</h2>
  
  <pre><code>&lt;bean id="SessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
            parent="AbstractSessionFactory" depends-on="EnvironmentHelper" &gt;
          &lt;property name="hibernateProperties"&gt;
  ...
  ...
  everything as expected
  ...
  &lt;/bean&gt;
  </code></pre>
  
  <h2>common/hibernate.xml:</h2>
  
  <pre><code>&lt;beans ... &gt;
  bean id="AbstractSessionFactory"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
              abstract="true"&gt;
          &lt;property name="mappingResources"&gt;
              &lt;list&gt;
                  &lt;value&gt;com/amazon/fresh/fbs/dao/hibernate/RtipData.hbm.xml&lt;/value&gt;
                  &lt;value&gt;com/amazon/fresh/fbs/dao/hibernate/Vendor.hbm.xml&lt;/value&gt;
                  &lt;value&gt;com/amazon/fresh/fbs/dao/hibernate/AdjustmentPeriod.hbm.xml&lt;/value&gt;
                  &lt;value&gt;com/amazon/fresh/fbs/dao/hibernate/DemandForecast.hbm.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="exposeTransactionAwareSessionFactory"&gt;
              &lt;value&gt;true&lt;/value&gt;
          &lt;/property&gt;
       &lt;/bean&gt;
  
       &lt;!-- Use Spring transactions for Hibernate --&gt;
       &lt;tx:annotation-driven transaction-manager="txManager" mode='proxy' proxy-target-class='true'/&gt; 
  
       &lt;bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="SessionFactory" /&gt;
       &lt;/bean&gt;
  
       &lt;aop:aspectj-autoproxy/&gt;
  &lt;/beans&gt;
  </code></pre>