如何获取Spring Data JPA存储库工厂?

时间:2015-11-03 15:40:17

标签: java spring spring-data spring-data-jpa

由于我没有得到my previous question的答案,我试图调整Spring documentation中给出的用于自定义存储库的示例。有一个方法getRepository(Class repositoryInterface)看起来像是映射我的存储库的正确位置覆盖:

public class MyRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
        extends JpaRepositoryFactoryBean<R, T, I> {

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

        return new MyRepositoryFactory<>(entityManager);
    }

    private static class MyRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

        private EntityManager entityManager;

        @Resource
        private Map<Class<?>, Class<?>> overrideRepositories;

        public MyRepositoryFactory(EntityManager entityManager) {
            super(entityManager);
            this.entityManager = entityManager;

            //Test
            overrideRepositories = new HashMap<>();
            overrideRepositories.put(CustomerRepository.class, Customer2Repository.class);
        }

        protected Object getTargetRepository(RepositoryMetadata metadata) {

            return super.getTargetRepository(metadata);
            // return new MyRepositoryImpl<T, I>((Class<T>)
            // metadata.getDomainClass(), entityManager);
        }

        protected Class<?> 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 <E> E getRepository(Class<E> repositoryInterface, Object customImplementation) {
            if (overrideRepositories != null) {
                Class<?> override = overrideRepositories.get(repositoryInterface);
                if (override != null) {
                    repositoryInterface = (Class<E>) override;
                }
            }
            return super.getRepository(repositoryInterface, customImplementation);
        }
    }
}

我的配置如下:@EnableJpaRepositories(repositoryFactoryBeanClass=MyRepositoryFactoryBean.class)

通常情况下,您会自动装配自己无法正常工作的存储库,因为有两个具有相同类型和I don't know how to tell Spring which one to use的接口。

如果我改为工厂自动装配,每次需要特定工厂时我都可以拨打getRepository。但是我怎么得到这个工厂? Spring Data JPA是否以某种方式将其公开为bean?我无法在谷歌上找到任何关于此的内容。或者这种做法完全错了?

1 个答案:

答案 0 :(得分:0)

您可以使用 ApplicationContext 实例来获取您的 MyRepositoryFactoryBean bean 类。您只需实现 ApplicationContextAware 接口即可访问 ApplicationContext 实例。

  public class myClass implements ApplicationContextAware{
    
        private static ApplicationContext ac;
        
            @Override
            public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                this.ac = applicationContext;
            }
        
        }

现在您可以使用 ac.getBean("MyRepositoryFactoryBean") 直接从 ApplicationContext 获取工厂。拥有该 bean 后,您可以对其调用 getRepository