Given the list of all spring data repositories in some class Bar
:
@Autowired
private List repositories;
How
@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);
}
}