我正在创建一个spring boot application,其中任何客户机都可以提交请求,这些请求可以是GET、PUT、POST、DELETE。
但是,在创建此应用程序时,我会收到以下错误:
***************************
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来注释@Service,@Repository,@Component,但是仍然会遇到同样的错误。
我甚至尝试过从这些答案中找到解决方案:
(1) Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found
(3) Parameter 0 of constructor in ..... Spring Boot
但我仍然不能解决我的问题。
发布于 2020-05-10 13:55:41
通过将限定符@Qualifier("fake demo")添加到public PersonService(@Qualifier("fake demo") PersonDao personDao)中,将搜索带有该限定符的bean在不存在的PersonService中注入。您也可以在PersonDao上声明这个限定符或删除它。我建议把它移除。此外,您还应该用@Repository对PersonDao进行注释,并扩展接口org.springframework.data.repository.Repository。
https://stackoverflow.com/questions/61713091
复制相似问题