spring framework : expected single matching bean but found 2

Viewed 14144

This super class DAO:

public class CrudDAO{

}

This child class:

@Repository
public class JnsTimeDao extends CrudDAO {

}

@Repository
public class BatchDAO extends CrudDAO {
}

this super service class

@Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {

    @Autowired
    protected D dao;
}

startup error:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.gp.dao.CrudDAO] is defined: expected single matching bean but found 2: batchDAO,jnsTimeDao

1 Answers

There are 2 beans of type CrudDAO. So, Spring won't be able to understand which bean to inject. Can be solved as follows

@Repository("jnsTimeDao")
public class JnsTimeDao extends CrudDAO {

}

@Repository("batchDao")
public class BatchDAO extends CrudDAO {
}

While injecting use @Qualifier

 @Transactional(readOnly = true)
    public abstract class CrudService<D extends CrudDAO> {

        @Autowired
        @Qualifier("batchDao")
        protected D dao;
    }
Related