【发布时间】:2020-08-26 00:19:32
【问题描述】:
我正在创建一个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 注释PersonDao.java,但我仍然遇到同样的错误。
我什至尝试了这些 SO 答案的解决方案:
(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
但我仍然无法解决我的问题。
【问题讨论】:
-
你对限定词
@Qualifier("fake demo") PersonDao personDao的意图是什么? -
PersonDao不是JpaRepository也不是spring bean,您必须将其定义为两者 -
好的@Lino,我知道了,我的应用程序开始成功运行
标签: java spring-boot