【问题标题】:Parameter 0 of constructor in ' ' required a bean of type ' ' that could not be found' ' 中构造函数的参数 0 需要一个无法找到的 ' ' 类型的 bean
【发布时间】:2020-08-26 00:19:32
【问题描述】:

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

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

***************************
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

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

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

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 注释PersonDao.java,但我仍然遇到同样的错误。

我什至尝试了这些 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

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

【问题讨论】:

  • 你对限定词@Qualifier("fake demo") PersonDao personDao的意图是什么?
  • PersonDao 不是JpaRepository 也不是spring bean,您必须将其定义为两者
  • 好的@Lino,我知道了,我的应用程序开始成功运行

标签: java spring-boot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 2019-10-20
    • 2019-03-21
    • 2019-05-30
    • 2020-02-23
    • 2021-02-02
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    相关资源
    最近更新 更多