I am creating a spring boot application
, wherein any client can submit the request, these request can be GET
, PUT
, POST
, DELETE
.
But while creating this application, I am getting the following errors:
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.idr.springboot.service.PersonService required a bean of type 'com.idr.springboot.dao.PersonDao' that could not be found. The following candidates were found but could not be injected: - User-defined bean Action: Consider revisiting the entries above or defining a bean of type 'com.idr.springboot.dao.PersonDao' in your configuration.
The structure of my application is:
PersonDao.java
package com.idr.springboot.dao; import com.idr.springboot.model.Person; import java.util.UUID; public interface PersonDao { int insertPerson(UUID id, Person person); default int insertPerson(Person person) { UUID id = UUID.randomUUID(); return insertPerson(id, person); } }
PersonService.java
package com.idr.springboot.service; import com.idr.springboot.dao.PersonDao; import com.idr.springboot.model.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class PersonService { private final PersonDao personDao; @Autowired public PersonService(@Qualifier("fake demo") PersonDao personDao) { this.personDao = personDao; } public int addPerson(Person person) { return personDao.insertPerson(person); } }
I know that many questions with the following error, are already been asked, but still I am not able to solve this.
Parameter 0 of constructor in com.idr.springboot.service.PersonService required a bean of type 'com.idr.springboot.dao.PersonDao' that could not be found.
I have tried to annotate PersonDao.java
with @Service
, @Repository
, @Component
, but still I am getting the same error.
I have even tried solutions from these SO answers :
(1) Parameter 0 of constructor in required a bean of type ‘java.lang.String’ that could not be found
(3) Parameter 0 of constructor in ….. Spring Boot
But still I am not able to resolve my issue.
Advertisement
Answer
By adding the qualifier @Qualifier("fake demo")
to public PersonService(@Qualifier("fake demo") PersonDao personDao)
a bean with that qualifier is searched to be injected in PersonService
which does not exist. You can declare this qualifier as well on PersonDao
or remove it. I would recommend removing it. In addition you should annotate PersonDao
with @Repository
and extend interface org.springframework.data.repository.Repository
.