简体   繁体   English

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

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

Given the list of all spring data repositories in some class Bar :鉴于某些 class Bar中的所有 spring 数据存储库的列表:

@Autowired
private List<Repository> repositories;

How can I find the repository for an existing domain class Foo in the above list?如何在上面的列表中找到现有域 class Foo的存储库?

Assuming that the following exists:假设存在以下情况:

@Entity
public class Foo {
  ...
}

and

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

Spring Data Commons contains a class Repositories that takes a ListableBeanFactory to 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 ,它使ListableBeanFactory查找在其中定义的所有存储库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 Repositories instance is created as late as possible during the ApplicationContext creation. 由于存储库实例正在进行一些严格的代理生成,因此必须确保在创建ApplicationContext期间尽可能晚地创建Repositories实例。 The preferred way is to implement ApplicationListener and create the instance by listening to the ContextRefreshedEvent . 首选方法是实现ApplicationListener并通过侦听ContextRefreshedEvent来创建实例。

In case you're writing a web application, the safest way to use Repositories is by bootstrapping the repositories in the ApplicationContext created by the ContextLoaderListener and place the Repositories (see the reference documentation of Spring MVC for details. 如果您正在编写Web应用程序,使用Repositories的最安全方法是引导ContextLoaderListener创建的ApplicationContextRepositories并放置Repositories (有关详细信息,请参阅Spring MVC的参考文档)

@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);
    }
}

The key to the solution is Spring's org.springframework.data.repository.core.support.DefaultRepositoryMetadata which provides the method getDomainType() . 解决方案的关键是Spring的org.springframework.data.repository.core.support.DefaultRepositoryMetadata ,它提供了方法getDomainType()

DefaultRepositoryMetadata needs the repository interface as constructor arg. DefaultRepositoryMetadata需要存储库接口作为构造函数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 . 因此,可以循环遍历所有现有存储库,检索存储库接口(这仍然是一个棘手的部分,因为存储库实例具有多个接口)并找到getDomainType()等于Foo.class的存储库接口。

You can use Spring's GenericTypeResolver to get the Entity class from your Repository.您可以使用 Spring 的GenericTypeResolver从您的存储库中获取实体 class。

Repository<Foo, String> fooRepository = repositories.stream()
    .filter(repository -> GenericTypeResolver
        .resolveTypeArguments(repository.getClass(), Repository.class)[0].equals(Foo.class))
    .findFirst().get();

This worked for me:这对我有用:

Repositories repositories = new Repositories(context);
CrudRepository repo = (CrudRepository) repositories.
            getRepositoryFor(entityClass).get();

暂无
暂无

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

相关问题 如何使用findBy Spring存储库在Java中的Map中检索数据 - How to retrieve data within a Map in java using findBy spring repository Spring Data JPA存储库检索多个计数 - Spring data jpa repository retrieve multiple counts 如何将不是实体的类的值提供给Spring数据中的存储库 - How to provide values from a class which is not an entity to a repository in spring data 我如何在 spring 2.2.4.RELEASE 中从类中检索 jpa-repository 实例 - how can i retrive jpa-repository instance from class in spring 2.2.4.RELEASE 使用Spring Data Common发布域事件时如何处理没有存储库的聚合根 - How to deal with the aggregate root without repository, when using Spring Data Common to publish domain events Spring Data JPA如何为实现公共接口的两个域类创建单个存储库? - Spring Data JPA How to create single repository for two domain classes implementing a common interface? Spring Data-如何在存储库片段中重用存储库? - Spring Data - How to reuse repository in repository fragment? 给定一个 Spring Data Rest URI,你如何找到关联的目标存储库? - Given a Spring Data Rest URI, How Do you find the associated target repository? 聚合 Spring 数据 Couchbase 存储库方法 - 如何查询给定属性的所有唯一值的列表? - Aggregate Spring Data Couchbase repository methods - how do I query for a list of all unique values for a given property? 如何在Spring应用程序中检索InMemoryUserDetailsManager实例? - How to retrieve the InMemoryUserDetailsManager instance in a Spring application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM