title: Hibernate Spring Integration, Tables Are Being Created, But SessionFactory is not Autowired
body: <p>I have integrated Spring and Hibernate, when i run my application using jetty plugin for maven, Hibernate creates tables but i my sessionFactory object is null, </p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;!-- The definition of the Root Spring Container shared by all Servlets 
      and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml,
              /WEB-INF/spring/security-context.xml
              &lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>servlet-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;!-- DispatcherServlet Context: defines this servlet's request-processing 
      infrastructure --&gt;
  
  &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
  &lt;annotation-driven /&gt;
  
  &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
      up static resources in the ${webappRoot}/resources directory --&gt;
  &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
  &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
      in the /WEB-INF/views directory --&gt;
  &lt;beans:bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
      &lt;beans:property name="suffix" value=".jsp" /&gt;
  &lt;/beans:bean&gt;
  
  &lt;context:component-scan base-package="com.fyp.ptma" /&gt;
  
  
  
  &lt;beans:bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" /&gt;
  
  &lt;beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /&gt;
  
  
  &lt;beans:bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;beans:property name="dataSource" ref="dataSource"&gt;&lt;/beans:property&gt;
      &lt;beans:qualifier value="sessionFactory"&gt;&lt;/beans:qualifier&gt;
      &lt;beans:property name="annotatedClasses"&gt;
          &lt;beans:list&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Application&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.ApplicationVersion&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Defect&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.DefectDetail&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Group&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Downloader&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Notification&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.RegisterStatus&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.User&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Invitation&lt;/beans:value&gt;
              &lt;beans:value&gt;com.fyp.ptma.model.Installation&lt;/beans:value&gt;
          &lt;/beans:list&gt;
      &lt;/beans:property&gt;
      &lt;beans:property name="hibernateProperties"&gt;
          &lt;beans:props&gt;
              &lt;beans:prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect
              &lt;/beans:prop&gt;
              &lt;beans:prop key="hibernate.show_sql"&gt;true&lt;/beans:prop&gt;
              &lt;beans:prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/beans:prop&gt;
          &lt;/beans:props&gt;
      &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  
  
  
  &lt;beans:bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;beans:property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>UserDao.java</strong></p>
  
  <pre><code>@Repository
  public class UserDao implements IUserDao {
  
  @Autowired
  @Qualifier(value="sessionFactory")
  private SessionFactory sessionFactory;
  
  public Session openSession() {
      return sessionFactory.getCurrentSession();
  }
  
  public Long save(Object objToSave) throws PersistenceException,
          ConstraintViolationException {
      Session session = null;
      try {
          session = this.openSession();
          session.beginTransaction();
          session.save(objToSave);
          session.getTransaction().commit();
  
      } catch (Exception e) {
          e.printStackTrace();
          session.getTransaction().rollback();
      } finally {
          // session.close();
      }
  
      return null;
  }
  
  public Long update(Object objToUpdate) throws PersistenceException {
      // TODO Auto-generated method stub
      return null;
  }
  
  public boolean delete(Object objToDelete) throws PersistenceException,
          ConstraintViolationException {
      // TODO Auto-generated method stub
      return false;
  }
  
  public List&lt;Application&gt; getAllApplications(User user)
          throws HibernateException {
      // TODO Auto-generated method stub
      return null;
  }
  
  public Application getApplication(User user) throws HibernateException {
      // TODO Auto-generated method stub
      return null;
  }
  
  public boolean authenticateLogin(User user) throws AuthenticationException,
          HibernateException {
  
      Session session = this.openSession();
  
      session.beginTransaction();
      Criteria criteria = session.createCriteria(User.class);
      criteria.add(Restrictions.eq("email", user.getEmail()));
  
      User userDB = (User) criteria.uniqueResult();
  
      return userDB.getPassword().equals(user.getPassword());
  
  }
  
  public User getUserByEmail(String email) throws HibernateException {
      Session session = this.openSession();
      session.beginTransaction();
      Criteria criteria = session.createCriteria(User.class);
      criteria.add(Restrictions.eq("email", email));
  
      User userDB = (User) criteria.uniqueResult();
      // session.close();
      return userDB;
  }
  
  }
  </code></pre>
  
  <p><strong>My Controller</strong></p>
  
  <pre><code>@ResponseBody
  @RequestMapping(value = "/processRegistration.html", method = RequestMethod.POST)
  public String processRegistration(@ModelAttribute("user") User user,
          Model model, BindingResult result) {
  
      UserDao uDao = DaoFactory.getUserDao();
      uDao.save(user);
  
      return "ok";
  }
  </code></pre>
  
  <p>when i call uDao.save in my controller , sessionFactory object of UserDao is null, but during initialisation i dont get any wiring error when i try my controller with below line,  </p>
  
  <blockquote>
    <p>ApplicationContext context = new
    AnnotationConfigApplicationContext("com.fyp.ptma.dao");</p>
  </blockquote>
  
  <p>it throw exception while submitting form:</p>
  
  <pre><code>: No matching bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>