title: Issue with Spring Data JPA - BeanEntityManagerFactory
body: <p>I recently started learning spring. I am trying a simple example with spring data jpa in a spring mvc project. I am getting the following error while deploying the war file in the tomcat.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creati
  ng bean with name '(inner bean)#584d15f2': Cannot resolve reference to bean 'ent
  ityManagerFactory' while setting constructor argument; nested exception is org.s
  pringframework.beans.factory.BeanCurrentlyInCreationException: Error creating be
  an with name 'entityManagerFactory': Requested bean is currently in creation: Is
   there an unresolvable circular reference?
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver
  .resolveReference(BeanDefinitionValueResolver.java:359)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver
  .resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
          at org.springframework.beans.factory.support.ConstructorResolver.resolve
  ConstructorArguments(ConstructorResolver.java:634)
          at org.springframework.beans.factory.support.ConstructorResolver.instant
  iateUsingFactoryMethod(ConstructorResolver.java:444)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBean
  Factory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:11
  19)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBean
  Factory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBean
  Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBean
  Factory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver
  .resolveInnerBean(BeanDefinitionValueResolver.java:299)
          ... 92 more
  Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: E
  rror creating bean with name 'entityManagerFactory': Requested bean is currently
   in creation: Is there an unresolvable circular reference?
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
  y.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
  y.getSingleton(DefaultSingletonBeanRegistry.java:223)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
  an(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
  (AbstractBeanFactory.java:194)
          at org.springframework.beans.factory.support.BeanDefinitionValueResolver
  .resolveReference(BeanDefinitionValueResolver.java:351)
          ... 100 more
  27-Sep-2015 17:56:45.304 INFO [http-apr-8080-exec-35] org.apache.catalina.startu
  p.HostConfig.deployWAR Deployment of web application archive D:\ApacheTomcat\apa
  che-tomcat-8.0.26\webapps\springTest.war has finished in 6,124 ms
  </code></pre>
  
  <p>My controller code is as follows,</p>
  
  <pre><code>package com.demo.repo;
  
  import com.demo.model.Customer;
  
  import java.text.DateFormat;
  import java.util.Date;
  import java.util.Locale;
  import java.util.Properties;
  
  import javax.activation.DataSource;
  import javax.persistence.EntityManagerFactory;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.stereotype.Controller;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import com.zaxxer.hikari.HikariConfig;
  import com.zaxxer.hikari.HikariDataSource;
  
  /**
   * Handles requests for the application home page.
   */
  @Controller
  @Configuration
  @EnableJpaRepositories("com.demo.repo")
  
  
  @EnableTransactionManagement
  public class HomeController {
  
      @Autowired
      customerRepository repository;
  
      private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  
      /**
       * Simply selects the home view to render by returning its name.
       */
  
      @Bean(destroyMethod = "close")
      DataSource dataSource(Environment env) {
      HikariConfig dataSourceConfig = new HikariConfig();
      dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
      dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
      dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
      dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
  
      return (DataSource) new HikariDataSource(dataSourceConfig);
      }
  
  
      @Bean
      LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, 
                                                              Environment env) {
      LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
      entityManagerFactoryBean.setDataSource((javax.sql.DataSource) dataSource);
      entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
      entityManagerFactoryBean.setPackagesToScan("com.demo.repo");
  
      Properties jpaProperties = new Properties();
  
      //Configures the used database dialect. This allows Hibernate to create SQL
      //that is optimized for the used database.
      jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
  
      //Specifies the action that is invoked to the database when the Hibernate
      //SessionFactory is created or closed.
      jpaProperties.put("hibernate.hbm2ddl.auto", 
              env.getRequiredProperty("hibernate.hbm2ddl.auto")
      );
  
      //Configures the naming strategy that is used when Hibernate creates
      //new database objects and schema elements
      jpaProperties.put("hibernate.ejb.naming_strategy", 
              env.getRequiredProperty("hibernate.ejb.naming_strategy")
      );
  
      //If the value of this property is true, Hibernate writes all SQL
      //statements to the console.
      jpaProperties.put("hibernate.show_sql", 
              env.getRequiredProperty("hibernate.show_sql")
      );
  
      //If the value of this property is true, Hibernate will format the SQL
      //that is written to the console.
      jpaProperties.put("hibernate.format_sql", 
              env.getRequiredProperty("hibernate.format_sql")
      );
  
      entityManagerFactoryBean.setJpaProperties(jpaProperties);
  
      return entityManagerFactoryBean;
      }
  
  
      @Bean
      JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory);
          return transactionManager;
      }
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
          logger.info("Welcome home! The client locale is {}.", locale);
  
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  
          String formattedDate = dateFormat.format(date);
  
          model.addAttribute("serverTime", formattedDate );
  
           repository.save(new Customer("Jack", "Bauer"));
           repository.save(new Customer("Chloe", "O'Brian"));
           repository.save(new Customer("Kim", "Bauer"));
           repository.save(new Customer("David", "Palmer"));
           repository.save(new Customer("Michelle", "Dessler"));
  
           for(Customer customer : repository.findAll())
           {
               System.out.println("Log Results ::  "+customer.toString());
           }
  
          return "myhome";
      }
  
  }
  </code></pre>
  
  <p>Can anyone suggest me what is wrong with my code and any suggestions to resolve the same.</p>