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接口
public interface IUserGroupRepository extends MongoRepository<UserGroup, Integer> {
UserGroup findById(Integer id);
}
报错内容如下:
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缺一不可。切记,切记。
Spring Boot 调用 MongoRepository时报org.springframework.beans.factory.NoSuchBeanDefinitionException错误的解决办法的更多相关文章
- spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...
- struts2和spring整合错误 org.springframework.beans.factory.BeanCreationException,已解决
先贴上错误 2018-8-16 23:41:10 org.springframework.context.support.ClassPathXmlApplicationContext prepareR ...
- springboot 报错 org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type 'com.example.service.HrService' available: 有没有大佬出个主意,我找了一天,刚入门springboot
话不多说先上图,这是启动类的配置,这里配置了@ComponentScan("我的mapper的接口") 接下来是我的项目结构截图 然后是service 的截图,我在这里加了注解@S ...
- nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException
You should autowire interface AbstractManager instead of class MailManager. If you have different im ...
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
1.错误描写叙述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -he ...
- 添加事务后 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type available
今天遇到了一个奇怪的问题 在没添加事务之前 所有的代码都是好的 , 当我添加了事务之后, 代码报错 org.springframework.beans.factory.NoSuchBeanDef ...
- Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "" available: expected at least 1 bean which qualifies as autowire candidate
项目使用spring, mybatis.因为分了多个模块,所以会这个模块引用了其它模块的现在,结果使用Junit测试的时候发现有两个模块不能自动注入dao和service问题.解决后在此记录一下. 解 ...
- Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException
今天在学习spring 框架的时候看着,很简单.但是在真正开始做的时候发现,异常一个接着一个,整的肚子都被搞大了. Exception in thread "main" org.s ...
- 解决org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined
在Spring配置文件中加入了支持注解,即<mvc:annotation-driven/> 重新启动服务器包 org.springframework.beans.factory.NoSuc ...
随机推荐
- Java并发编程(四)-- Java内存模型
Java内存模型 前面讲到了Java线程之间的通信采用的是共享内存模型,这里提到的共享内存模型指的就是Java内存模型(简称JMM),JMM决定一个线程对共享变量的写入何时对另一个线程可见.从抽象的角 ...
- spring项目出现无法加载主类
问题:eclipse总是运行之前的项目,新改变的项目内容,不运行.于是我想要是清理缓存,网上说project--->clean指定的项目就可以 但是clean后就无法加载主类了,项目上还出现了红 ...
- array中key所对应的value
获取数组中的键对应的值 class Test{ public function index(){ $info = array( 'name' => 'daicr', 'age' => 24 ...
- xcode升级至9.0之后,新建xib报错: Safe Area Layout Guide Before IOS 9.0
直接翻译是:iOS 9.0前安全区域布局指南. 字面意思就是safe area布局应用在iOS 9之前的版本上了. 解决方法: 1,我们可以把safe area勾掉,不用safe area布局,如下图 ...
- CodeForces 456-C Boredom
题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...
- 向excel中循环插入值
import xlrd #导入excel读模块 from xlutils import copy #导入copy模块 book = xlrd.open_workbook('tb_base_buildi ...
- Alpha冲刺(4/10)——2019.4.27
所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(4/10)--2019.4.27 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪 ...
- HTML常用标签1
1 html html:超文本标记(标签)语言 通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则 浏览器渲染的顺序:从上到下,从左到右 对于不同的浏览器,对同一标签可能会有不完全 ...
- Vue(二十二)vuex小案例(官网计数案例整合)
1.使用 vue-cli 创建项目(具体操作可以参考前面的文章) ... 2.下载 vuex - npm install vuex -S 3.将 vuex 添加到项目中 (1)在项目中创建store文 ...
- web的几种轮播
我们在开发当中经常用到轮播.我在这里总结了一下几种,仅供参考: 第一种: 1.jQuery:用display :none/block控制的一种轮播: // CSS部分 #igs { margin: 3 ...