title: Can instantiate hibernate session Factory directly but cannot do it through spring
body: <p>I have started learning hibernate and spring through an assignment in which i am trying to use session factory instance through spring. I understood the hibernate part but just cant go on with spring. I have tried numerous tutorials and examples but just cant get my spring working. Although it works when i instantiate it directly. 
  Here are the problem related details of my project...</p>
  
  <p><strong>applicationContext.xml</strong> (inside WEB-INF)</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:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;context:component-scan
      base-package="com.nagarro.training.assignment6.dao.entity.Student" /&gt;
  
  
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="configLocation" value="classpath:hibernate.cfg.xml"&gt;&lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;
              hibernate.dialect=org.hibernate.dialect.HSQLDialect
          &lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean id="studentDAO"
      class="com.nagarro.training.assignment6.dao.impl.StudentDAOImplementation"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Edit: including changes as suggested</strong>
  <strong>StudentDAOImplementaion.java</strong> (the file where it is being used )</p>
  
  <pre><code>public class StudentDAOImplementation implements StudentDAO {
  
      /**
       * single instance of hibernate session
       */
      @Autowired
      private SessionFactory sessionFactory;
  
      // HibernateUtil.getSessionFactory().openSession()
  
      /**
       * @param sessionFactory
       */
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      /**
       * @return the sessionFactory
       */
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      private Session getSession() {
          return this.sessionFactory.getCurrentSession();
      }
          /**
       * @return list of all the students
       */
      public List&lt;Student&gt; getStudentList() {
  
          System.out.println(this.getSession().getStatistics());
  
          return (List&lt;Student&gt;) this.getSEssion.createQuery("from Student")
                  .list();
      }
  
  }
  </code></pre>
  
  <p>And here is the snip of my jar files in lib folder:
  <img src="https://i.stack.imgur.com/IbYkv.png" alt="enter image description here"></p>
  
  <p>I think that i dont need to include hibernate files and beans as it is working fine without spring. Its the spring i cant get to work.I have tried many different implementation from the web but i just cannot get it working. It just says null pointer exception on **       System.out.println(sessionFactory.getStatistics());** line in StudentDAOImplementation.</p>
  
  <p>Test class that calls StudentDAO</p>
  
  <pre><code>public class Test {
  
  public static void main(String[] args) {
      StudentDAOImplementation sd = new StudentDAOImplementation();
  
      List&lt;Student&gt; list = sd.getStudentList();
  
      for(Student s : list) {
          System.out.println(s.getName());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Stack trace</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at StudentDAOImplementation.getStudentList(StudentDAOImplementation.java:116)
      at Test.main(Test.java:13)
  </code></pre>