简体   繁体   English

如何获取 Spring Data JPA Repository Factory?

[英]How to get the Spring Data JPA Repository Factory?

Since I got no answer to my previous question I tried to tweak the example given in the Spring documentation for customizing repositories.由于我没有回答我之前的问题,我尝试调整Spring 文档中给出的示例以自定义存储库。 There ist a Method getRepository(Class repositoryInterface) which looks like It ist the right place to map my repository Overrides:有一个方法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);
        }
    }
}

I configured it like this: @EnableJpaRepositories(repositoryFactoryBeanClass=MyRepositoryFactoryBean.class)我是这样配置的: @EnableJpaRepositories(repositoryFactoryBeanClass=MyRepositoryFactoryBean.class)

Normally you would autowire the repositories themselves which doesn't work because there are two Interfaces with the same Type and I don't know how to tell Spring which one to use .通常你会自动装配存储库本身,这不起作用,因为有两个接口具有相同的类型, 我不知道如何告诉 Spring 使用哪个接口

If I autowire the factory instead, I can call getRepository each time I need a specific one.如果我改为自动装配工厂,我可以getRepository每次需要特定工厂时调用getRepository But how do I get this factory?但是我怎么得到这个工厂? Does Spring Data JPA somehow expose this as a bean? Spring Data JPA 是否以某种方式将其公开为 bean? I can't find anything on google concerning this.我在谷歌上找不到任何关于此的信息。 Or is this approach entirely wrong?或者这种方法完全错误?

You can use the ApplicationContext instance to get your MyRepositoryFactoryBean bean class.您可以使用ApplicationContext实例来获取MyRepositoryFactoryBean bean 类。 All you have to do is implement the ApplicationContextAware interface in order to get access to the ApplicationContext instance.您所要做的就是实现ApplicationContextAware接口,以便访问ApplicationContext实例。

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

Now you can use ac.getBean("MyRepositoryFactoryBean") to get the factory directly from the ApplicationContext .现在您可以使用ac.getBean("MyRepositoryFactoryBean")直接从ApplicationContext获取工厂。 Once you have that bean you can call getRepository on it.一旦你有了那个 bean,你就可以在它上面调用getRepository

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM