Spring Boot 调用 MongoRepository时报org.springframework.beans.factory.NoSuchBeanDefinitionException错误的解决办法

这个问题整整折腾了我两天,现在记录下来,希望可以帮助和我一样,遇到相同问题的小伙伴。

项目是分层的(Intellij IDEA中的模块Module),有API(Core)层,Service&Dao,Common,Model,上一张项目结构图。(不要在意为什么Service和Dao放在一起。这不是重点。)

API中包括了Controller,Mongodb在Service&Dao层中,也就是API层,调用Service&Dao层,我敢用性命担保,所有注释,逻辑,都没有错,但是运行SpringBoot的时候,偏偏报错。(找不到依赖,就是注入失败。)

代码中继承了MongoRepository接口

1
2
3
public interface IUserGroupRepository extends MongoRepository<UserGroup, Integer> {
    UserGroup findById(Integer id);
}

 报错内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2017-06-28 10:11:52.423  WARN 8648 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userGroupController': Unsatisfied dependency expressed through field 'userGroupRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-06-28 10:11:52.426  INFO 8648 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-06-28 10:11:52.446  INFO 8648 --- [           main] utoConfigurationReportLoggingInitializer :
 
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-28 10:11:52.591 ERROR 8648 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :
 
***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Field userGroupRepository in com.iboxpay.ops.api.controller.UserGroupController required a bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' that could not be found.
 
 
Action:
 
Consider defining a bean of type 'com.iboxpay.nosql.mongodb.IUserGroupRepository' in your configuration.

  解决办法如下:

@SpringBootApplication(scanBasePackages = {"com.iboxpay.service", "com.iboxpay.ops.api", "com.iboxpay.nosql"})
@EnableMongoRepositories(basePackages = {"com.iboxpay.nosql"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

因为我的项目是分层的,SpringBoot的@SpringBootApplication注解要添加扫描包,否则有的地方可能会注入失败。另外一点比较重要,需要使有和Mongodb的@EnableMongoRepositories注解,并指定要扫描的包(IUserGroupRepository所在的包,即继承

MongoRepository接口的接口中所在的包。)
上面两个com.iboxpay.nosql缺一不可。切记,切记。

 

posted @   ゞ追忆o0ゞ  阅读(37847)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示