1

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:

enter image description here

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

(2) Spring - Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be found

(3) Parameter 0 of constructor in ..... Spring Boot

But still I am not able to resolve my issue.

3
  • What is your intention of the qualifier @Qualifier("fake demo") PersonDao personDao ? Commented May 10, 2020 at 13:50
  • 1
    PersonDao is not a JpaRepository nor a spring bean, you have to define it to be both Commented May 10, 2020 at 13:58
  • Okay @Lino, I got it, and my application started running successfully Commented May 10, 2020 at 14:01

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Make sure the model, controller, and repository folders are at the same level as the main SpringbootApplication.java file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.