title: Spring @Autowired SessionFactory is always null
body: <p>I just wrote a simple test with spring+hibernate, but I'm getting a null pointer exception in this test as <code>sessionFactory</code> is always null. Then I tried to invoke the method <code>getBean()</code> to get <code>sessionFactory</code>. I found that it works, but <code>@autowired</code> doesn't. Why?</p>
  
  <pre><code>private ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  protected SessionFactory sessionFactory=(SessionFactory)applicationContext.getBean("sessionFactory");
  </code></pre>
  
  <p>Any help is appreciated; thanks.</p>
  
  <p>Below is the content of relevant files:</p>
  
  <p>Here is my <code>applicationContext.xml</code></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans         
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        
    http://www.springframework.org/schema/aop         
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        
    http://www.springframework.org/schema/context         
    http://www.springframework.org/schema/context/spring-context-4.0.xsd        
    http://www.springframework.org/schema/tx         
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache-4.0.xsd"&gt;
  &lt;!-- &lt;bean id="..." class="..."&gt; collaborators and configuration for this 
      bean go here &lt;/bean&gt; &lt;bean id="..." class="..."&gt; collaborators and configuration 
      for this bean go here &lt;/bean&gt; more bean definitions go here --&gt;
  
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url"
          value="jdbc:mysql://localhost:3306/desserthouse?autoReconnect=true" /&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="1234" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="packagesToScan" value="edu.nju.dessertHouse.model" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
              &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;prop key="hibernate.connection.autocommit"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="autowiredAnnotationBeanPostProcessor"
      class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /&gt;
  &lt;context:component-scan base-package="edu.nju.dessertHouse" /&gt;
  &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="*User" propagation="REQUIRED" /&gt;
          &lt;tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" /&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  
  &lt;aop:config&gt;
      &lt;aop:pointcut id="serviceOperation"
          expression="execution( * edu.nju.dessertHouse.service..*Service.*(..))" /&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is my test <code>MemberDaoImpl</code>:</p>
  
  <pre><code>@Repository
  public class MemberDaoImpl implements MemberDao {
  
  @Autowired
  protected SessionFactory sessionFactory;
  
  protected Session session;
  
  
  protected static MemberDaoImpl memberDao=new MemberDaoImpl();
  
  public Session getSession(){
      return sessionFactory.openSession();
  }
  
  @Override
  public void resigter(Member member) {
      try {  
          session=getSession();
          Transaction tx=session.beginTransaction();
          session.save(member);
          tx.commit();
          session.close();
          sessionFactory.close();
          System.out.println("ok");
      }  
      catch (Exception e) {  
          e.printStackTrace();
      }   
  }
  
  public static void main(String[] args){
      Member member=new Member();
      member.setMemberid(1);
      member.setCreditCard("1");
      member.setMemberCard("1");
      member.setLocation("1");
      member.setMname("1");
      member.setPassword("1");
      member.setSex("1");
      member.setPoint(1);
      Calendar calendar=Calendar.getInstance();
      calendar.set(Calendar.YEAR,1995);
      calendar.set(Calendar.MONTH,7-1);  
      calendar.set(Calendar.DAY_OF_MONTH,6);
      Date date=new Date(calendar.getTimeInMillis());
      member.setBirthday(date);
      memberDao.resigter(member);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Why is <code>sessionFactory</code> null? I have tried many methods, but it still doesn't work.</p>