代码改变世界

springboot available: expected at least 1 bean which qualifies as autowire candidate奇葩问题

2019-09-24 14:01  hikoukay  阅读(21355)  评论(0编辑  收藏  举报
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.
UnsatisfiedDependencyException: Error creating bean with name 'apiUserController': Unsatisfied dependency expressed
through field 'apiMutiHttpClientUtil'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.crt.openapi.utils.ApiMutiHttpClientUtil' available: expected at least 1 bean which qualifies
as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Description: Field apiMutiHttpClientUtil in com.crt.openapi.apiinterface.controller.base.CommonController required
a bean of type
'com.crt.openapi.utils.ApiMutiHttpClientUtil' 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.crt.openapi.utils.ApiMutiHttpClientUtil' in your configuration. 总是提示无法注入Service或者Dao中的Bean! 后来经研究发现,SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类。这个类的位置很关键: 如果Application类所在的包为:com.demo.module,则只会扫描com.demo.module包及其所有子包,如果service或dao所在包不在com.demo.module及其子包下,
则不会被扫描! 所以需在application启动类中加上 @MapperScan(
"com.*.dao")//加上你项目的dao或service所在文件位置即可 @SpringBootApplication