Home » Java » java – Spring Autowire cannot find implementation for R2DBC Repository in Generic Class-Exceptionshub

java – Spring Autowire cannot find implementation for R2DBC Repository in Generic Class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


public abstract class BaseService<R extends IChanakyaR2dbcRepository<E, String>, E, D> {

    protected R repository;

    public abstract D convertToDTO(E entity);

    public abstract E convertToEntity(D DTO);

    public Flux<D> findAll() {
        return repository.findAll().map(this::convertToDTO);

    public Mono<D> findById(String id) {
        return repository.findById(id).map(this::convertToDTO);

    public Mono<D> create(D DTO) {
        return repository.insert(convertToEntity(DTO)).map(this::convertToDTO);

    public Mono<D> update(D DTO) {
        return repository.save(convertToEntity(DTO)).map(this::convertToDTO);

    public Mono<Void> delete(D DTO) {
        return repository.delete(convertToEntity(DTO));

I have the above class as an abstract class from which all my base services class will inherit.

public class ClusterService extends BaseService<IChanakyaR2dbcRepository<ClusterEntity, String>, ClusterEntity, ClusterDTO> {

    ModelMapper modelMapper;

    public ClusterDTO convertToDTO(ClusterEntity entity) {
        return modelMapper.map(entity, ClusterDTO.class);

    public ClusterEntity convertToEntity(ClusterDTO DTO) {
        return modelMapper.map(DTO, ClusterEntity.class);


This is my service class.

public interface IChanakyaR2dbcRepository<T, ID> extends ReactiveCrudRepository<T, ID> {

     * Inserts a given entity. Use the returned instance for further operations as the save operation might have changed the
     * entity instance completely.
     * @param entity must not be {@literal null}.
     * @return {@link Mono} emitting the inserted entity.
     * @throws IllegalArgumentException in case the given {@literal entity} is {@literal null}.
    <S extends T> Mono<S> insert(S entity);


This is my repository class.

@Transactional(readOnly = true)
public class ChanakyaR2dbcRepository<T, ID> extends SimpleR2dbcRepository<T, ID> {

    private final RelationalEntityInformation<T, ID> entity;
    private final DatabaseClient databaseClient;
    private final R2dbcConverter converter;
    private final ReactiveDataAccessStrategy accessStrategy;

    public ChanakyaR2dbcRepository(RelationalEntityInformation entity, DatabaseClient databaseClient, R2dbcConverter converter, ReactiveDataAccessStrategy accessStrategy) {
        super(entity, databaseClient, converter, accessStrategy);
        this.entity = entity;
        this.databaseClient = databaseClient;
        this.converter = converter;
        this.accessStrategy = accessStrategy;

    public <S extends T> Mono<S> insert(S objectToSave) {

        Assert.notNull(objectToSave, "Object to insert must not be null!");

        try {
            return this.databaseClient.insert()
        } catch(DataIntegrityViolationException diex) {
            String errMsg = String.format("Entity {} already exists in database", this.entity);
            log.error(errMsg, diex);
            return Mono.error(new IllegalStateException(errMsg));


This is my repository implementation.

When I run the application it, I get the following error.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘clusterService’: Unsatisfied dependency expressed through field ‘repository’;
nested exception is

org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
‘com.appdynamics.sum.chanakya.repository.base.IChanakyaR2dbcRepository’ available: expected at least 1 bean which qualifies
as autowire candidate. Dependency annotations:

The above autowire doesn’t work, but when I autowire IChanakyaR2dbcRepository<ClusterEntity, String> in another componenet directly ( not via a generic type ). It works fine.

What is the correct way to do such a thing? I want to have BaseService implementation.

I know if you create an interface that extends IChanakyaR2dbcRepository<ClusterEntity, String> and pass that class as the generic type to BaseService then it will work fine. But that would mean creating such empty classes for every entity I have which is not an ideal solution.

How to&Answers: