title: Spring Data JPA: Instantiate multiple repositories from same repository interface definitions, each pointing to a different database
body: <p>I have a set of entity classes and repository definitions and N databases, all of which have the schema. How can I instantiate N repositories pointing to each of the N databases.</p>
  
  <pre><code>@Entity
  public class Student {
  }
  
  public interface StudentRepository extends JpaRepository&lt; Student, Long&gt; {
  }
  </code></pre>
  
  <p>Making as many copies of repository interfaces as the number of databases (<a href="http://www.baeldung.com/spring-data-jpa-multiple-databases" rel="nofollow">http://www.baeldung.com/spring-data-jpa-multiple-databases</a>) and using dynamic datasource routing (<a href="http://spring.io/blog/2007/01/23/dynamic-datasource-routing/" rel="nofollow">http://spring.io/blog/2007/01/23/dynamic-datasource-routing/</a>) were two solutions I found. </p>
  
  <p>But, what I need is something like</p>
  
  <pre><code>@Component
  public class Foo {
  
      @Autowired
      StudentRepository studentRepositoryForDatabase1;
      @Autowired
      StudentRepository studentRepositoryForDatabase2;
  
  }
  </code></pre>
  
  <p>Is this feasible?</p>