dimanche 31 mai 2015

Spring autowire repository by entity type

All of my entities implement the interface IEntity.

I then created an interface for all repositories like so:

@NoRepositoryBean
public interface IRepository<T extends IEntity> extends CrudRepository<T, Long>

I have some very generic code that takes in any entity type and needs the repository for that entity type. I'm trying to use spring's ApplicationContext.getBean(Class<T>) method to fetch the appropriate repository for the entity passed in like this:

@Autowired
ApplicationContext context;

IRepository<? extends IEntity> getRepository(final IEntity entity) {
    Class<IRepository<? extends IEntity>> clazz = ???????;
    return context.getBean(clazz);
}

How to I tell it what class I need to autowire based on?





Aucun commentaire:

Enregistrer un commentaire