SpringBoot无法注入JpaRepository接口 (找不到指定Bean)

本文详细解析了Spring框架中依赖注入失败的错误信息,主要针对GenTableRepository与GenTableServiceImpl之间的依赖问题。通过检查@EnableJpaRepositories注解及配置正确的扫描路径,解决了JpaRepository无法被Spring容器识别的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

警告信息

2020-07-15 14:09:28.821 WARN 11732 — [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘genController’: Unsatisfied dependency expressed through field ‘iGenTableService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘genTableServiceImpl’: Unsatisfied dependency expressed through field ‘genTableRepository’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.bella.dao.GenTableRepository’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

报错信息
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-15 14:09:29.199 ERROR 11732 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field genTableRepository in com.wondersgroup.healthcloud.service.impl.GenTableServiceImpl required a bean of type 'com.wondersgroup.healthcloud.dao.GenTableRepository' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.wondersgroup.healthcloud.dao.GenTableRepository' in your configuration.

我的代码

Bean没有问题
Spring容器中找不到这个Bean

解决思路

  • GenTableRepository -> Bean的定义没有问题
  • GenTableServiceImpl -> Bean的定义没有问题

两个Bean的定义都没问题 , 但是 GenTableServiceImpl 却无法在Spring容器中找到 GenTableRepository 这个Bean 。
注意 : 我们使用的是JpaRepository (spring-data-jpa)的功能 , 可能是项目框架中没有定义 JpaRepository 扫描包的位置 或 定义的位置和我们Class的位置不一致导致我们的 Bean 无法的注入到 Spring 容器中。

解决方案

  1. 项目中搜索 @EnableJpaRepositories 注解 (一般都会有,没有就自己添加)
  2. 添加我们的扫描包路径 (或将我们定义的JpaRepository放到项目指定的路径下)
  3. 重启项目就应该可以了
## 指定JpaRepositry扫描包的位置
@EnableJpaRepositories(basePackages = {"com.bella.dao.repository", "com.bella.dao"})

## 扫描的包可以定义多个 , basePackages 是一个 String 数组 (一定要保证你的Reposity在扫描路径中)
## 如果项目中没有定义 @EnableJpaRepositories , 可以在Application启动类上添加此注解 (或者自己定义一个 JpaConfig Bean 也可以)

## 指定Entity的扫描位置 (如果不指定 Entity Bean 也无法使用)
@EntityScan(basePackages = { "com.bella.domain" })
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐米多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值