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

后端 未结 3 1948
夕颜
夕颜 2020-12-24 07:47

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

@Autowired
private List repositories;

How

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 08:23

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

    DefaultRepositoryMetadata needs 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.

提交回复
热议问题