title: Why can't spring boot/data automagically configure multiple data sources?
body: <p>I have a spring boot app that connects to two databases. <strong>Everything works fine.</strong></p>
  
  <p>My package structure looks like the following</p>
  
  <pre><code>- db
    - bar
      - BarDbConfig.java
      - domain
        - BarModel.java
      - repo
        - BarRepo.java
    - foo
      - FooDbConfig.java
      - domain
        - FooModel.java
      - repo
        - FooRepo.java
  
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>spring.jpa.database=default
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  spring.jpa.hibernate.ddl-auto=none
  spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  
  #first db
  bar.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db3.data
  bar.datasource.username=admin
  bar.datasource.password=admin
  bar.datasource.driver-class-name=org.h2.Driver
  
  #second db
  foo.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db2.data
  foo.datasource.username=admin
  foo.datasource.password=admin
  foo.datasource.driver-class-name=org.h2.Driver
  </code></pre>
  
  <p>BarDbConfig.java</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          entityManagerFactoryRef = "barEntityManagerFactory",
          transactionManagerRef = "barTransactionManager"
  )
  public class BarDbConfig {
  
      @Bean
      @Primary
      @ConfigurationProperties(prefix = "bar.datasource")
      public DataSource barDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Primary
      @Bean
      public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
              EntityManagerFactoryBuilder builder,
              @Qualifier("barDataSource") DataSource dataSource
      ) {
          return builder.dataSource(dataSource)
                  .packages(BarDbConfig.class.getPackage().getName())
                  .persistenceUnit("barPersistenceUnit")
                  .build();
      }
  
      @Primary
      @Bean
      public PlatformTransactionManager barTransactionManager(
              @Qualifier("barEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
          return new JpaTransactionManager(entityManagerFactory);
      }
  }
  </code></pre>
  
  <p>FooDbConfig.java</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          entityManagerFactoryRef = "fooEntityManagerFactory",
          transactionManagerRef = "fooTransactionManager"
  )
  public class FooDbConfig {
  
      @Bean
      @ConfigurationProperties(prefix = "foo.datasource")
      public DataSource fooDataSource() {
          return DataSourceBuilder.create().build();
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean fooEntityManagerFactory(
              EntityManagerFactoryBuilder builder,
              @Qualifier("fooDataSource") DataSource dataSource
      ) {
          return builder.dataSource(dataSource)
                  .packages(FooDbConfig.class.getPackage().getName())
                  .persistenceUnit("fooPersistenceUnit")
                  .build();
      }
  
      @Bean
      public PlatformTransactionManager fooTransactionManager(
              @Qualifier("fooEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
          return new JpaTransactionManager(entityManagerFactory);
      }
  }
  </code></pre>
  
  <p>However, I was wondering if all this boilerplate code was really necessary?</p>
  
  <ul>
  <li>Isn't there any way to remove all the <code>LocalContainerEntityManagerFactoryBean</code> and <code>PlatformTransactionManager</code> stuff?</li>
  <li>Is there any reason why it is necessary to provide all those explicit <code>Qualifier</code>-names?</li>
  <li>Can't spring boot find and autoconfigure the datasources  by it self??</li>
  <li>What would be the absolute minimal code required to connect to two databases utilizing convention over configuration?</li>
  </ul>