SpringBoot扫描不到类,注入失败A component required a bean of type 'XXService' that could...
SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!
“Application类”是指SpringBoot项目入口类。
这个类的位置很关键:
如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!
即, 把Application类放到dao、service所在包的上级,com.boot.Application
知道这一点非常关键,大多数情况下bean无法注入进来都是这个原因引起的。
本人的错误原因是:entity,service,serviceImpl,controller等这些包和Application.java SpringBoot程序的入口不在同一个包且不在Application.java的子包中。
原因是:SpringBoot运行时所加载的包是Application.java本包及其子包的代码。所以根本扫描不到其他包,你怎么改注解都是错误的。
原因之二:就是bean加载顺序不对,比方说你的拦截器是在service加载之前就生成了,application在生成拦截器bean的时候怎么能找到service这个bean呢.
SpringBoot扫描不到类,注入失败A component required a bean of type 'XXService' that could...的更多相关文章
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- 运行springboot项目报错:Field userMapper in XX required a bean of type 'xx' that could not be found.
运行springboot项目报错: *************************** APPLICATION FAILED TO START ************************** ...
- Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b
环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 目录结构: 可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了E ...
- 2. springboot启动报错:Field userMapper in com.service.UserService required a bean of type 'com.dao.UserMapper' that could not be found.
报错信息: 2018-06-25 14:26:17.103 WARN 49752 --- [ restartedMain] ationConfigEmbeddedWebApplicationCon ...
- spring 类注入失败,解决之道
1.今天偶尔发现的问题,如果你在一个类上面用了注解@Async,spring的异步注解之后,发现如果别的类用@Autowired导入这个类时会失败! 解决办法:用了@Async无非是想方便的用异步操作 ...
- 67. @Transactional的类注入失败【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] Spring的代理模式有两种:java自带的动态代理模式和cglib代理模式,cglib代码模式适用于没有接口的类,而java自带适用于接口类,默 ...
- spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案
经常出现这问题一定是非spring生态圈的@标签 没被spring引入,如mybatis等 因为在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动 ...
- springboot jpa mongodb 整合mysql Field in required a bean of type that could not be found Failed to load ApplicationContext
1.完整报错 *************************** APPLICATION FAILED TO START *************************** Descripti ...
- SpringBoot扫描包提示找不到mapper的问题
SpringBoot扫描包问题 报错信息:Consider defining a bean of type in your configuration 方法一: 使用注解 @ComponentScan ...
随机推荐
- php的注释方法
注释是每个程序员学习时的基础,我们通过可以注释来备注一信息.增加代码的可读性.下面我们就为大家介绍一下PHP的注释方法. 1, // 这是单行注释 2,# 这也是单行注释 3,/* */多行注释块 ...
- MVC http://stackoverflow.com/tags/model-view-controller/info
About model-view-controller Model–View–Controller (MVC) is an architectural pattern used in software ...
- 第一节 初识RabbitMQ
原文:第一节 初识RabbitMQ 版权声明:未经本人同意,不得转载该文章,谢谢 https://blog.csdn.net/phocus1/article/details/87280120 1.什么 ...
- 自定义 MessageBox 组件
效果: 公共组件页面: js部分: <script> export default { props: { title: { type: String ...
- vue-router中的router-link的active-class
vue-router中的router-link的active-class 在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...
- Mysql 数据库默认值选 ''" 、Null和Empty String的区别
两者的查询方式不一样:NULL值查询使用is null/is not null查询,而empty string可以使用=或者!=.<.>等算术运算符,这点算是最主要的区别了. 对于myis ...
- JavaSE---多线程---线程的创建、启动
1.概述 1.1 Java中使用Thread类表示线程: 所有的线程对象必须是Thread类 或 其子类的实例: 每条线程的作用:完成一定的任务: Java中使用run方法来封装线程执行体 ...
- Windows下JDK安装步骤 + eclipse集成配置
一.JDK环境配置 1.解压到java目录下 2.路径和上面路径一致 3.配置path路径 4.查看版本 二.Eclipse配置 1.编译版本 2.运行环境 3.默认字集 4.字体大小
- c++11 中的注意事项
1. C++11标准中让类的析构函数默认也是noexcept(true)的. 但如果显式地为析构函数指定了noexcept,或者类的基类或成员有noexcept(false)的析构函数,析构函数就不会 ...
- hdu 6092 Rikka with Subset (集合计数,01背包)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...