title: Spring 4 + Hibernate 4 IllegalArgumentException: Property 'sessionFactory' is required
body: <p>I'm writing a DAL with Spring Data and Hibernate but I'm running into a IllegalArgumentException exception which is stopping my work.</p>
  
  <p>Here is the <code>DALConf.java</code> class which contains DataSource and persistence exception translation processor configurations</p>
  
  <pre><code>package my.dal.config;
  
  import java.util.Properties;
  
  import javax.annotation.Resource;
  import javax.sql.DataSource;
  
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.apache.commons.dbcp2.BasicDataSourceFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  
  
  @Configuration
  @ComponentScan(basePackages = { "my.dal" })
  @PropertySource("classpath:dbconnection.properties")
  public class DALConfig {
  
      private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";  
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";  
      private static final String PROPERTY_NAME_DATABASE_URL = "db.url";  
      private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";  
      private static final String PROPERTY_NAME_POOL_INITIAL_SIZE = "pool.initialsize";
      private static final String PROPERTY_NAME_POOL_MAX_IDLE = "pool.maxidle";
  
      @Resource
      private Environment environment;   
  
      @Bean
      public DataSource dataSource() throws Exception
      {
          Properties props = new Properties();
  
          props.put("driverClassName", environment.getProperty(PROPERTY_NAME_DATABASE_DRIVER));
          props.put("url", environment.getProperty(PROPERTY_NAME_DATABASE_URL));
          props.put("username", environment.getProperty(PROPERTY_NAME_DATABASE_USERNAME));
          props.put("password", environment.getProperty(PROPERTY_NAME_DATABASE_PASSWORD));
          props.put("initialSize", environment.getProperty(PROPERTY_NAME_POOL_INITIAL_SIZE));
          props.put("maxIdle", environment.getProperty(PROPERTY_NAME_POOL_MAX_IDLE));
  
          BasicDataSource bds = BasicDataSourceFactory.createDataSource(props);
  
          return bds; 
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor()
      {
          PersistenceExceptionTranslationPostProcessor b = new PersistenceExceptionTranslationPostProcessor();
          return b;
      }   
  }
  </code></pre>
  
  <p>Then here is the <code>HibernateConfig.class</code> which contains Hibernate configurations</p>
  
  <pre><code>package my.dal.config;
  
  import java.util.Properties;
  import javax.annotation.Resource;
  import javax.sql.DataSource;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
  import org.springframework.orm.hibernate4.HibernateTransactionManager;
  import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @Configuration
  @ComponentScan(basePackages = { "my.dal" })
  @PropertySource("classpath:hibernate.properties")
  @EnableTransactionManagement
  public class HibernateConfig {
  
      private static final String PROPERTY_NAME_DAL_CLASSES_PACKAGE = "hibernate.dal.package";  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.showsql";
  
      @Resource
      private Environment environment;  
  
      @Autowired
      DataSource dataSource;
  
      @Bean
      public SessionFactory sessionFactory()
      {
  
          LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
  
          lsfb.setPackagesToScan(environment.getProperty(PROPERTY_NAME_DAL_CLASSES_PACKAGE));
  
          Properties hibernateProperties = new Properties();
          hibernateProperties.put("dialect", environment.getProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
          hibernateProperties.put("show_sql", environment.getProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
  
          lsfb.setHibernateProperties(hibernateProperties);
          lsfb.setDataSource(dataSource);
          return lsfb.getObject();
  
      }
  
      @Bean 
      public HibernateExceptionTranslator hibernateExceptionTranslator(){ 
        return new HibernateExceptionTranslator(); 
      }
  
  
      @Bean
      public HibernateTransactionManager transactionManager()
      {
          // HERE THE EXCEPTION IS THROWN
          HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory());
          return htm;
      }   
  
  }
  </code></pre>
  
  <p>This is the DAO <code>UserDAO.java</code></p>
  
  <pre><code>package my.dal.dao;
  
  import my.models.dal.User;
  
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  
  @Repository
  public class UserDAO
  {
  
      private SessionFactory sessionFactory;
  
      @Autowired
      public UserDAO(SessionFactory sessionFactory) {
          this.sessionFactory=sessionFactory;
      }
  
      @Transactional
      public int insert(User user) {
          return (Integer) sessionFactory.getCurrentSession().save(user);
      }
  
      @Transactional
      public User getByUsername(String username) {
          return (User) sessionFactory.getCurrentSession().get(User.class, username);
      }
  
      @Transactional
      public void update(User user) {
          sessionFactory.getCurrentSession().merge(user);
      }
  
      @Transactional
      public void delete(String username) {
          User u = getByUsername(username);
          sessionFactory.getCurrentSession().delete(u);
      }
  
  }
  </code></pre>
  
  <p>Finally, this is the test class <code>DALTest.java</code></p>
  
  <pre><code>package my.dal.tests;
  
  import static org.junit.Assert.assertTrue;
  import my.dal.config.DALConfig;
  import my.dal.config.HibernateConfig;
  import my.dal.dao.UserDAO;
  import my.models.dal.User;
  import org.hibernate.SessionFactory;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @ContextConfiguration(classes = { DALConfig.class, HibernateConfig.class})
  @RunWith(SpringJUnit4ClassRunner.class)
  public class DALTest {
  
      @Autowired
      SessionFactory sessionFactory;
  
      @Test
      public void testGetUser() {
          UserDAO userDAO = new UserDAO(sessionFactory);
          User user = null;
          user = userDAO.getByUsername("mrossi");
  
          assertTrue(null != user);
      }
  
  }
  </code></pre>
  
  <p>The execution of the test ends with the following exception</p>
  
  <pre><code>...
  Caused by: java.lang.IllegalArgumentException: Property 'sessionFactory' is required
      at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:247)
      at org.springframework.orm.hibernate4.HibernateTransactionManager.&lt;init&gt;(HibernateTransactionManager.java:130)
      at my.dal.config.HibernateConfig.transactionManager(HibernateConfig.java:66)
  ...
  </code></pre>
  
  <p>It is thrown at this line</p>
  
  <pre><code>HibernateTransactionManager htm = new HibernateTransactionManager(sessionFactory());
  </code></pre>
  
  <p>It seems like Spring cannot instantiate the sessionFactory bean but I don't know what could be the problem...</p>
  
  <p>What do you think about that?</p>
  
  <p>Thank you</p>