title: How to get the Spring Data JPA Repository Factory?
body: <p>Since I got no answer to <a href="https://stackoverflow.com/questions/33496823/primary-equivalent-for-autowired-spring-jpa-repositories">my previous question</a> I tried to tweak the example given in the <a href="http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html" rel="nofollow noreferrer">Spring documentation</a> for customizing repositories. There ist a Method <code>getRepository(Class repositoryInterface)</code> which looks like It ist the right place to map my repository Overrides:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyRepositoryFactoryBean&lt;R extends JpaRepository&lt;T, I&gt;, T, I extends Serializable&gt;
          extends JpaRepositoryFactoryBean&lt;R, T, I&gt; {
  
      protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
  
          return new MyRepositoryFactory&lt;&gt;(entityManager);
      }
  
      private static class MyRepositoryFactory&lt;T, I extends Serializable&gt; extends JpaRepositoryFactory {
  
          private EntityManager entityManager;
  
          @Resource
          private Map&lt;Class&lt;?&gt;, Class&lt;?&gt;&gt; overrideRepositories;
  
          public MyRepositoryFactory(EntityManager entityManager) {
              super(entityManager);
              this.entityManager = entityManager;
  
              //Test
              overrideRepositories = new HashMap&lt;&gt;();
              overrideRepositories.put(CustomerRepository.class, Customer2Repository.class);
          }
  
          protected Object getTargetRepository(RepositoryMetadata metadata) {
  
              return super.getTargetRepository(metadata);
              // return new MyRepositoryImpl&lt;T, I&gt;((Class&lt;T&gt;)
              // metadata.getDomainClass(), entityManager);
          }
  
          protected Class&lt;?&gt; getRepositoryBaseClass(RepositoryMetadata metadata) {
  
              // The RepositoryMetadata can be safely ignored, it is used by the
              // JpaRepositoryFactory
              // to check for QueryDslJpaRepository's which is out of scope.
              return JpaRepository.class;
          }
  
          @SuppressWarnings("unchecked")
          @Override
          public &lt;E&gt; E getRepository(Class&lt;E&gt; repositoryInterface, Object customImplementation) {
              if (overrideRepositories != null) {
                  Class&lt;?&gt; override = overrideRepositories.get(repositoryInterface);
                  if (override != null) {
                      repositoryInterface = (Class&lt;E&gt;) override;
                  }
              }
              return super.getRepository(repositoryInterface, customImplementation);
          }
      }
  }
  </code></pre>
  
  <p>I configured it like this: <code>@EnableJpaRepositories(repositoryFactoryBeanClass=MyRepositoryFactoryBean.class)</code></p>
  
  <p>Normally you would autowire the repositories themselves which doesn't work because there are two Interfaces with the same Type and <a href="https://stackoverflow.com/questions/33496823/primary-equivalent-for-autowired-spring-jpa-repositories">I don't know how to tell Spring which one to use</a>.</p>
  
  <p>If I autowire the factory instead, I can call <code>getRepository</code> each time I need a specific one. But how do I get this factory? Does Spring Data JPA somehow expose this as a bean? I can't find anything on google concerning this. Or is this approach entirely wrong?</p>