java 如何检索给定域类的 spring 数据存储库实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14266089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:40:56  来源:igfitidea点击:

How to retrieve spring data repository instance for given domain class?

javaspringjpaspring-dataspring-data-jpa

提问by Udo

Given the list of all spring data repositories in some class Bar:

鉴于某些类中所有 spring 数据存储库的列表Bar

@Autowired
private List<Repository> repositories;

How can I find the repository for an existing domain class Fooin the above list?

如何Foo在上述列表中找到现有域类的存储库?

Assuming that the following exists:

假设存在以下情况:

@Entity
public class Foo {
  ...
}

and

public interface FooRepository extends JpaRepository<Foo, String> {}

回答by Oliver Drotbohm

Spring Data Commons contains a class Repositoriesthat takes a ListableBeanFactoryto find all repository beans defined in it and exposes an API to obtain these instances by domain class (through ….getRepository(Class<?> type)).

Spring Data Commons 包含一个类Repositories,该类使用 aListableBeanFactory来查找其中定义的所有存储库 bean,并公开一个 API 以通过域类(通过….getRepository(Class<?> type))获取这些实例。

This class should be used with care. As there's some serious proxy generation going on for the repository instances you have to make sure the Repositoriesinstance is created as late as possible during the ApplicationContextcreation. The preferred way is to implement ApplicationListenerand create the instance by listening to the ContextRefreshedEvent.

这个类应该小心使用。由于存储库实例正在进行一些严重的代理生成,因此您必须确保在Repositories创建过程中尽可能晚地创建实例ApplicationContext。首选方法是ApplicationListener通过侦听ContextRefreshedEvent.

In case you're writing a web application, the safest way to use Repositoriesis by bootstrapping the repositories in the ApplicationContextcreated by the ContextLoaderListenerand place the Repositories(see the reference documentationof Spring MVC for details.

如果您正在编写 Web 应用程序,最安全的使用方法RepositoriesApplicationContext在由ContextLoaderListener和放置创建的存储库中引导存储库Repositories(有关详细信息,请参阅Spring MVC的参考文档

回答by Deepak

@Service
public class GenericRepository {

    @Autowired
    private WebApplicationContext appContext;

    Repositories repositories = null;

    public GenericRepository() {
        repositories = new Repositories(appContext);
    }

    public JpaRepository getRepository(AbstractPersistable entity) {
        return (JpaRepository) repositories.getRepositoryFor(entity.getClass());
    }

    public Object save(AbstractPersistable entity) {
        return getRepository(entity).save(entity);
    }

    public Object findAll(AbstractPersistable entity) {
        return getRepository(entity).findAll();
    }

    public void delete(AbstractPersistable entity) {
        getRepository(entity).delete(entity);
    }
}

回答by Udo

The key to the solution is Spring's org.springframework.data.repository.core.support.DefaultRepositoryMetadatawhich provides the method getDomainType().

解决方案的关键是 Springorg.springframework.data.repository.core.support.DefaultRepositoryMetadata提供的方法getDomainType()

DefaultRepositoryMetadataneeds the repository interface as constructor arg. So one can loop over all existing repositories, retrieve the repository interface (which is still a tricky part because the repository instance has more than one interface) and find the one where getDomainType()equals Foo.class.

DefaultRepositoryMetadata需要存储库接口作为构造函数 arg。因此,可以遍历所有现有存储库,检索存储库接口(这仍然是一个棘手的部分,因为存储库实例具有多个接口)并找到getDomainType()等于 的那个Foo.class