title: sessionfactory always returns null in Spring hibernate xml based configuration
body: <p>i want to make a spring-hibernate configuration in my project having xml based configuration. When printing sessionFactory object it should return a value like some memory address but it gives null value.</p>
  
  <p>i want to use@Autowire annotation to inject sessionFactory in the bean.
  i am going to use hibernate managed transactions. </p>
  
  <p>my code is </p>
  
  <p>spring-config.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:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation=   
      http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context 
  </code></pre>
  
  <p><a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd" rel="nofollow noreferrer">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>" ></p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  
  &lt;context:property-placeholder location="classpath:application.properties" /&gt;
  &lt;bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClassName" value="${database.driverClassName}" /&gt;
      &lt;property name="url" value="${database.url}" /&gt;
      &lt;property name="username" value="${database.username}" /&gt;
      &lt;property name="password" value="${database.password}" /&gt;       
  &lt;/bean&gt;
  
   &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}
     &lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
     &lt;/bean&gt;
      &lt;bean id="abc" class="pkg.A"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>here is my interface </p>
  
  <p>Abc.java</p>
  
  <pre><code>package pkg;
  
  public interface Abc {
    public void m1();
  
  }
  </code></pre>
  
  <p>interface implementation</p>
  
  <p>A.java</p>
  
  <pre><code>            package pkg;    
  
              import org.hibernate.SessionFactory;
              import org.springframework.beans.factory.annotation.Autowired;
              import org.springframework.stereotype.Repository;
  
              @Repository
              public class A implements Abc {
  
                  @Autowired
                  SessionFactory sessionFactory;
  
  
  
                  public SessionFactory getSessionFactory() {
                      System.out.println("sys factory : "+sessionFactory);
                      return sessionFactory;
                  }
  
  
  
                  public void setSessionFactory(SessionFactory sessionFactory) {
                      this.sessionFactory = sessionFactory;
                  }
  
  
  
                  public void m1() {
                      System.out.println("session factory  obj : "+sessionFactory);
  
                  }
  
              }
  </code></pre>
  
  <p>it gives me     null </p>
  
  <p>tomcat output :</p>
  
  <pre><code>            Jul 21, 2017 8:38:47 PM org.springframework.web.context.ContextLoader initWebApplicationContext
              INFO: Root WebApplicationContext: initialization completed in 9610 ms
              Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
              INFO: Starting ProtocolHandler ["http-nio-8082"]
              Jul 21, 2017 8:38:47 PM org.apache.coyote.AbstractProtocol start
              INFO: Starting ProtocolHandler ["ajp-nio-8011"]
              Jul 21, 2017 8:38:47 PM org.apache.catalina.startup.Catalina start
              INFO: Server startup in 24169 ms
              session factory obj : null
  </code></pre>
  
  <p>Here is jsp file from this i am calling sessionfactory object like </p>
  
  <pre><code>                &lt;%@page import="pkg.A"%&gt;
              &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
                  pageEncoding="ISO-8859-1"%&gt;
              &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
              &lt;html&gt;
              &lt;head&gt;
              &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
              &lt;title&gt;Insert title here&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
  
              &lt;h2&gt;H2 heading&lt;/h2&gt;
  
              &lt;%
  
              A a = new A();
              a.m1();
  
              %&gt;
              &lt;/body&gt;
              &lt;/html&gt;
  </code></pre>