springboot available: expected at least 1 bean which qualifies as autowire candidate奇葩問題


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM