title: spring boot assign specific DataSource to JpaRepository
body: <p>I have two datasources that I'm trying to assign a specific datasource to each JpaRepositories. I'm using the spring boot framework. The primary datasource is used about 90% of the time while the secondary datasource is used about 10% so it would be nice to default to the primary and only assign the secondary datasource when required.  I tried using the docs here <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html" rel="nofollow noreferrer">https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html</a> , but I dont think its exactly what I need.  Any tips would be great!</p>
  
  <pre><code>spring.datasource.configuration.url=jdbc:postgresql://localhost:5432/mydatabase
  spring.datasource.configuration.username=dockerusername
  spring.datasource.configuration.password=dockerpassword
  spring.datasource.configuration.driver-class-name=org.postgresql.Driver
  
  spring.datasource.cached.url=jdbc:hsqldb:mem:main
  spring.datasource.cached.driver-class-name=org.hsqldb.jdbc.JDBCDriver
  
  spring.datasource.initialize=false
  spring.jpa.database=default
  spring.jpa.hibernate.ddl-auto=update
  </code></pre>
  
  <p>config file</p>
  
  <pre><code>@Configuration
  @ComponentScan({"com.praeses.gov"})
  public class Config {
  @Bean
  @ConfigurationProperties("spring.datasource.configuration")
  public DataSourceProperties configDataSourceProperties() {
      return new DataSourceProperties();
  }
  
  @Bean
  @ConfigurationProperties("spring.datasource.configuration")
  public DataSource configDataSource() {
      return configDataSourceProperties().initializeDataSourceBuilder().build();
  }
  
  @Bean
  @Primary
  @ConfigurationProperties("spring.datasource.cached")
  public DataSourceProperties cachedDataSourceProperties() {
      return new DataSourceProperties();
  }
  
  @Bean
  @Primary
  @ConfigurationProperties("spring.datasource.cached")
  public DataSource cachedDataSource() {
  return cachedDataSourceProperties().initializeDataSourceBuilder().build();
      }
  </code></pre>
  
  <p>repository that uses the primary datasource</p>
  
  <pre><code>@Qualifier("spring.datasource.cached")
  @Repository("spring.datasource.cached")
  public interface GeoitemRepository extends JpaRepository&lt;Geoitem, String&gt; {
  }
  </code></pre>
  
  <p>repository that uses the secondary datasource</p>
  
  <pre><code>@Qualifier("spring.datasource.configuration")
  @Repository("spring.datasource.configuration")
  public interface GeoitemhistoryRepository extends JpaRepository&lt;Geoitemhistory, String&gt; {
  }
  </code></pre>