“”中构造函数的参数0需要找不到“”类型的Bean

时间:2020-05-10 13:48:00

标签: java spring-boot

我正在创建一个Spring Boot应用程序,其中任何客户端都可以提交CRUD请求。

但是在创建此应用程序时,出现错误:

构造函数的参数0 com.idr.springboot.service.PersonService需要一个类型为bean的bean 找不到“ com.idr.springboot.dao.PersonDao”。

我的代码

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);
    }
}

我知道已经提出了许多带有以下错误的问题,但仍然无法解决。

我尝试用PersonDao.java@Service@Repository注释@Component,但仍然出现相同的错误。

我什至尝试了这些SO答案的解决方案:

(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

但是我仍然无法解决我的问题。

1 个答案:

答案 0 :(得分:2)

通过将限定符@Qualifier("fake demo")添加到public PersonService(@Qualifier("fake demo") PersonDao personDao)来搜索具有该限定符的bean,以将其注入不存在的PersonService中。您也可以在PersonDao上声明此限定符或将其删除。我建议将其删除。另外,您应该用PersonDao注释@Repository并扩展接口org.springframework.data.repository.Repository