Spring Boot 中构造函数的参数0需要类型为“”的Bean,但找不到该Bean

vsdwdz23  于 2022-12-18  发布在  Spring
关注(0)|答案(2)|浏览(245)

我正在创建一个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.

我的应用程序的结构是:

个人道.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);
    }

}

个人服务.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,但仍然得到相同的错误。
我甚至尝试过这些答案的解决方案:
(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
但我还是无法解决我的问题。

kmb7vmvb

kmb7vmvb1#

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

e0bqpujr

e0bqpujr2#

确保model、controller和repository文件夹与主www.example.com文件处于同一级别SpringbootApplication.java

相关问题