spring boot 多模块管理时,依赖注入bean失败
来源:互联网 发布:熊猫杀姐姐知乎 编辑:程序博客网 时间:2024/05/17 02:13
spring boot 多模块管理时,依赖注入bean失败的解决办法:
问题描述:
编码时没有错误,maven打包也没有问题,在运行时出现了异常,例如:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.test.itobc.web.repository.SysDictRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
...
分析:
首先我们已经明确了是依赖注入是未找到相应的bean,例如找不到jpa中的SysDictRepository。换句话说肯定是该bean未被容器扫描到,未加载。解决办法也就是告诉容器扫描加载。话说回来要怎么搞呢?
解决:
度娘或谷哥后,普遍的解决办法也就是在入口启动类处加上组件扫描配置就好了,例如:
@ComponentScan("cn.test.itobc.web")就这样指定基包名就好了,你可以试一试。
完整样子:
@SpringBootApplication@ComponentScan("cn.test.itobc.web")public class LawOfficeApplication { public static void main(String[] args){ SpringApplication.run(LawOfficeApplication.class); }}
当我们如上所述修改后还是有类似的异常信息时,这时有一个小细节需要您注意一下,就是入口启动类的位置。建议启动类位于包名的根目录。怎么理解这句话呢?还是举例来说明吧:
例如:
有一个模块A,包名为cn.test.itobc.web.a,本包名为根目录,在其下有类、子包...
有一个模块B, 包名为cn.test.itobc.web.b,本包名为根目录,在其下有类、子包...
现在B依赖着A,假如我们的启动类位于cn.test.itobc.web.b下,这时就需要我们把本LawOfficeApplication类往根目录的包中提升,将LawOfficeApplication提升到cn.test.itobc.web下即可。
亲,试一试吧!
祝君好运!-- Love is share.
.
- spring boot 多模块管理时,依赖注入bean失败
- 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- 采用Spring管理Bean和依赖注入
- spring boot项目依赖spring mvc项目注入失败问题
- spring boot 依赖注入
- Spring Boot 依赖注入
- 详细介绍采用Spring管理Bean和依赖注入方式
- spring的管理bean和依赖注入的原理剖析
- Spring--5.bean管理 依赖注入(xml)
- spring boot Filter 注入bean
- Spring boot 手动注入bean
- Spring 依赖注入失败原因
- spring依赖注入失败原因
- spring boot service注入失败
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- 使用MockMvc测试带有异步方法所踩的坑及解决办法
- git的使用
- linux -- tcpdump
- Delta RPMs disabled because /usr/bin/applydeltarpm not installed
- teamviewer 在 ubuntu 上的实现
- spring boot 多模块管理时,依赖注入bean失败
- GoEasy web推送
- Spring AOP几种方式的使用
- mybatis Invalid bound statement (not found)
- java调用python文件中的方法
- DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
- 微服务后如何做一次系统梳理
- 数组转arrayaList的最高效方法
- Maven打包跳过单元测试