title: Spring Boot NOT to make JPA database connection on statup
body: <p>I have a Spring Boot + Groovy application that requires the Database Connection to be made per-session or per-tenant basis. 
  When implementing the solution using Hibernate only it works, however when adding spring-boot-starter-data-jpa and switching to JPA a database connection is attempted on start up which fails because there is no connection settings defined yet.</p>
  
  <p>I have a gradle dependencies block that looks like this:</p>
  
  <pre><code>dependencies {
      compile "org.springframework.boot:spring-boot-starter-web"
      compile "org.springframework.boot:spring-boot-starter-data-jpa"
  
      compile "org.codehaus.groovy:groovy-all:2.3.3"
  
      compile "org.springframework:spring-core:${springVersion}"
      compile "org.springframework:spring-webmvc:${springVersion}"
      compile "org.springframework:spring-orm:${springVersion}"
      compile "org.springframework:spring-aop:${springVersion}"
      compile "org.springframework:spring-context-support:${springVersion}"
      compile "org.hibernate:hibernate-entitymanager:${hibernateVersion}"
      compile "com.oracle:ojdbc7:12.1.0.1"
      compile "org.apache.commons:commons-dbcp2:2.0.1"
      compile "cglib:cglib:3.1"
  ...
  }
  </code></pre>
  
  <p>And my Application.groovy looks like this:</p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan
  @EnableTransactionManagement
  class Application
  {
      final Logger aLog = Logger.getLogger(Application.class)
  
      @Autowired
      DatabaseSettings dbSettings
  
      @Bean(destroyMethod="close")
      @Lazy
      public DataSource dataSource()
      {
          aLog.info "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; CREATING DATA SOURCE &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;"
          new BasicDataSource(driverClassName: dbSettings.driverClassName,    
                              url: dbSettings.dbUrl, username: dbSettings.dbUser, password: dbSettings.dbPass, 
                              maxTotal: 10, maxWaitMillis: 10000, maxIdle:10, defaultAutoCommit:false)
      }
  
      @Bean
      @Lazy
      public LocalContainerEntityManagerFactoryBean entityManagerFactory()
      {
          Properties hqlProps =   ['hibernate.dialect': this.dbSettings.dialect, 'hibernate.temp.use_jdbc_metadata_defaults': false,
                                   'hibernate.show_sql': this.dbSettings.showSql, 'hibernate.format_sql': this.dbSettings.formatSql, 
                                   'hibernate.use_sql_comments': this.dbSettings.useSqlComments] as Properties
  
          aLog.info "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; CREATING ENTITY MANAGER FACTORY &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;"
  
          LocalContainerEntityManagerFactoryBean em   =   new LocalContainerEntityManagerFactoryBean()
  
          em.setDataSource(dataSource())
          em.setPackagesToScan(this.dbSettings.getPackagesToScan())
          em.setJpaVendorAdapter(new HibernateJpaVendorAdapter())
          em.setJpaProperties(hqlProps)
          em
      }
  
      @Bean
      @Lazy
      public JpaTransactionManager transactionManager()
      {
          aLog.info "&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; CREATING TRANSACTION MANAGER &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;"
          new JpaTransactionManager(entityManagerFactory: entityManagerFactory().getObject())     
      }
  
      static void main(args) throws Exception
      {
          ApplicationContext appCtx = SpringApplication.run(Application.class, args)
      }
  }
  </code></pre>
  
  <p>I have removed the Scopes configuration for simplicity.</p>
  
  <p>The point is that all relevant Beans are annotated as @Lazy, however at start up I see the messages regarding the Data Source and Entity Manager being created.</p>
  
  <p>Debugging the code looks like at some point HibernateJpaAutoConfigurer is called and this class have @Autowired the DataSource in thus causing the instantiation of these beans on start up.</p>
  
  <p>My question is: How can I prevent Spring Boot to attempt a Database Connection on start up?</p>
  
  <p>Thanks in advance </p>