简体   繁体   中英

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

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

@Autowired
private List<Repository> repositories;

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

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) ).

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. The preferred way is to implement ApplicationListener and create the instance by listening to the 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.

@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() .

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 .

You can use Spring's GenericTypeResolver to get the Entity class from your Repository.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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