You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot报错:QuestionService需QuestionDao Bean未找到,求助

问题解决:无法找到QuestionDao Bean导致构造器注入失败

错误信息

com.example.service.QuestionService的构造器第0个参数需要类型为'com.example.Dao.QuestionDao'的Bean,但未找到。

核心问题及修复步骤

1. 包名大小写不匹配(最关键原因)

Java包名规范为全小写,你的代码中存在大小写不一致:

  • QuestionDao的实际包应为com.example.dao(小写d)
  • 主类的ComponentScan中写的是com.example.Dao(大写D)

修复:
修改主类的ComponentScan包名,统一为小写:

@ComponentScan(basePackages={"com.example.controller","com.example.demo","com.example.dao","com.example.model","com.example.service"})

或者直接删除@ComponentScan——@SpringBootApplication默认会扫描主类所在包(com.example.demo)及其所有子包,只要其他类的包是com.example.demo.controller、com.example.demo.dao这类结构,无需手动指定扫描范围。

2. Controller中的拼写错误

QuestionController中声明依赖时写错了类名:

private QuestionSerice questionService; // 少了字母v,应为QuestionService

修复:修正类名拼写:

private QuestionService questionService;

3. 确保Dao接口的注解和泛型正确

  • 确认QuestionDao上的@Repository注解无误,且泛型Question是导入的com.example.model.Question,而非其他类型。
  • 检查QuestionDao的import语句:
import com.example.model.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

4. 移除主类中冗余的Dao声明

主类里的private QuestionDao questionDao;属于冗余代码,直接删除即可,不影响依赖注入。

验证修复

完成以上修改后重启Spring Boot应用,Spring即可正确扫描并创建QuestionDao的Bean,进而完成QuestionService的构造器注入。

内容的提问来源于stack exchange,提问作者Deepa Lakshmi.l

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:25:55