title: @Autowired SessionFactory returning null spring in Java configuration
body: <p>I m having null pointer exception in sessionFactory when i try to use DI in sessionFactory I have to turn spring 3 project into Spring 4 with no xml. I dont know what the problem i m facing, SessionFactory doesnot autowired at all. when i try to test case generic doa Add Method</p>
  
  <p>here is my Configuration File for bean</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableWebMvc
  @ComponentScan(basePackages = "io.github.bibekshakya35.ehealth")
  public class EhealthCofiguration {
  
  @Bean
  public ViewResolver getViewResolver() {
      InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
      viewResolver.setViewClass(JstlView.class);
      viewResolver.setPrefix("/WEB-INF/view/");
      viewResolver.setSuffix(".jsp");
      return viewResolver;
  }
  
  @Bean(name = "dataSource")
  public javax.sql.DataSource getDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName("org.postgresql.Driver");
      dataSource.setUrl("jdbc:postgresql://localhost:5432/ehealth");
      dataSource.setUsername("test");
      dataSource.setPassword("test123");
      return dataSource;
  }
  
  @Autowired
  @Bean(name = "sessionFactory")
  public SessionFactory getSessionFactory(javax.sql.DataSource dataSource) {
      LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
      sessionBuilder.scanPackages("io.github.bibekshakya35.ehealth.model");
      sessionBuilder.addProperties(getHibernateProperties());
      return sessionBuilder.buildSessionFactory();
  }
  
  private Properties getHibernateProperties() {
      Properties properties = new Properties();
      properties.put("hibernate.show_sql", "true");
      properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
      properties.put("hibernate.hbm2ddl.auto", "update");
      return properties;
  }
  
  @Autowired
  @Bean(name = "transactionManager")
  public HibernateTransactionManager getTransactionManager(
          SessionFactory sessionFactory) {
      HibernateTransactionManager transactionManager = new HibernateTransactionManager(
              sessionFactory);
  
      return transactionManager;
  }
  }
  </code></pre>
  
  <p>to load this class i have created </p>
  
  <pre><code>public class EhealthWebAppIntializer implements WebApplicationInitializer {
  
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
      AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
      applicationContext.register(EhealthCofiguration.class);
      ServletRegistration.Dynamic dispatcher =servletContext.addServlet("SpringDispatcher", new DispatcherServlet(applicationContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
  }
  
  }
  </code></pre>
  
  <p>this is my generic doa class where i m injecting SessionFactory when it try to access <code>sessionFactory.getCurrentSession();</code> NPE is occur,</p>
  
  <pre><code>@Repository
  @Transactional
  public class HibernateDAO&lt;T extends Serializable&gt; implements IGenericDao&lt;T&gt; {
  
  private Class&lt;T&gt; clazz;
  
  Session session;
  
  @Autowired
  SessionFactory sessionFactory;
  
  private static final Logger LOG = Logger.getLogger(HibernateDAO.class.getName());
  
  @Override
  public void setClazz(Class&lt;T&gt; clazzToSet) {
      this.clazz = clazzToSet;
  }
  
  @Override
  public void create(T entity) {
      session = getCurrentSession();
      LOG.log(Level.INFO, "inside create entity and you just bind your session to the current one{0}", session.toString());
      session.saveOrUpdate(entity);
      LOG.info("saved");
      session.flush();
      session.refresh(entity);
  }
  
  
  
  protected Session getCurrentSession() {
      return sessionFactory.getCurrentSession();
  }  
  }
  </code></pre>
  
  <p>Here is my stack trace for NPE</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at io.github.bibekshakya35.ehealth.DAO.genericdao.HibernateDAO.getCurrentSession(HibernateDAO.java:115)
  at io.github.bibekshakya35.ehealth.DAO.genericdao.HibernateDAO.create(HibernateDAO.java:85)
  at io.github.bibekshakya35.ehealth.service.impl.user.main(user.java:46)
  </code></pre>
  
  <p>here is my user entity</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User implements Serializable, AbstractEntity {
  
  
  @Id
  @Column(name = "username",unique = true)
  private String userName;
  
  @NotNull(message = "password cannot be empty")
  @Column(name = "users_password")
  private String userPassword;
  
  @Embedded
  private UserProfile userProfile;
  
  @Embedded
  private AuditInfo auditInfo;
  
  @Enumerated(EnumType.STRING)
  private UserType userType;
  
  @Column(name = "is_active")
  private boolean active = true;
  
  //getter n setter
  
  }
  </code></pre>