首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“”中构造函数的参数0需要一个无法找到的类型的bean

“”中构造函数的参数0需要一个无法找到的类型的bean
EN

Stack Overflow用户
提问于 2020-05-10 13:48:00
回答 1查看 1.8K关注 0票数 1

我正在创建一个spring boot application,其中任何客户机都可以提交请求,这些请求可以是GETPUTPOSTDELETE

但是,在创建此应用程序时,我会收到以下错误:

代码语言:javascript
复制
***************************
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.

我的应用程序结构是:

PersonDao.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}

我知道很多问题都有以下错误,都已经被问到了,但我仍然无法解决这个问题。

代码语言:javascript
复制
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.

我尝试用@Service@Repository@Component来注释@Service@Repository@Component,但是仍然会遇到同样的错误。

我甚至尝试过从这些答案中找到解决方案:

(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

但我仍然不能解决我的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-10 13:55:41

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61713091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档