title: Spring Boot 2.0.0 cycle bean dependency EntityManger and SessionFactory
body: <p>I have problem with new Spring Boot version 2.0.0.
  I need to create SessionFactory bean, for that I need Spring to inject EntityManager.    </p>
  
  <pre><code>package cz.moravec;
  import cz.moravec.provisioning.Provisioner;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.domain.EntityScan;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Profile;
  import org.springframework.transaction.annotation.Transactional;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceUnit;
  
  @SpringBootApplication
  @EntityScan("cz.moravec.data")
  public class Main {
  
  //    @Bean
  //    public CountryDao countryDao() {
  //        return new CountryDao();
  //    }
  //
  //    @Bean
  //    public TownDao townDao() {
  //        return new TownDao();
  //    }
  
  
      @Autowired
      @Bean
      @Transactional
      public SessionFactory sessionFactory(EntityManager entityManager) {
          Session session = entityManager.unwrap(Session.class);
          return session.getSessionFactory();
      }
  
      @Profile({"devel", "test"})
      @Bean(initMethod = "doProvision")
      public Provisioner provisioner() {
          return new Provisioner();
      }
  
  
      public static void main(String[] args) {
  
          SpringApplication app = new SpringApplication(Main.class);
          ApplicationContext ctx = app.run(args);
  
  //        CountryDao countryDao = ctx.getBean(CountryDao.class);
  
  //        List&lt;Country&gt; countries = countryDao.getCountries();
  
  //        UsersDao usersDao = ctx.getBean(UsersDao.class);
  //
  //        List&lt;User&gt; users = usersDao.getAllUsers();
  //        System.out.println(users);
  
      }
  
  }
  </code></pre>
  
  <p>This code works when using <strong>Spring Boot 1.5.11.RELEASE</strong>. But does not with <strong>Spring Boot 2.0.0.RELEASE</strong>.</p>
  
  <p>When I run code ApplicationContext is not created because of cycle dependency.</p>
  
  <p>There is output from console.</p>
  
  <pre><code>19:44:31.282 [main] WARN  o.s.c.a.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionFactory' defined in cz.moravec.Main: Unsatisfied dependency expressed through method 'sessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#0': Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
      Disconnected from the target VM, address: '127.0.0.1:11537', transport: 'socket'
      19:44:31.286 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLoggingListener - 
  
      Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
      19:44:31.288 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 
  
      ***************************
      APPLICATION FAILED TO START
      ***************************
  
      Description:
  
      The dependencies of some of the beans in the application context form a cycle:
  
      ┌─────┐
      |  sessionFactory defined in cz.moravec.Main
      ↑     ↓
      |  org.springframework.orm.jpa.SharedEntityManagerCreator#0
      └─────┘
  </code></pre>
  
  <p>Maven pom.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;cz.moravec&lt;/groupId&gt;
      &lt;artifactId&gt;semester_project&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.0.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-logging&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
              &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>