關於SpringBoot bean沒法注入的問題(與文件包位置有關)改變自動掃描的包

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [D:\IDEA\hyper-cloud\hyper-cloud-demo\build\classes\main\com\hyper\mapper\UserMapper.class]: Unsatisfied dependency

沒法注入Dao中的Bean!
解決分析後來經研究發現,SpringBoot項目的Bean裝配默認規則是根據Application類所在的包位置從上往下掃描! 「Application類」是指SpringBoot項目入口類。這個類的位置很關鍵:
若是Application類所在的包爲:io.github.gefangshuai.app
,則只會掃描io.github.gefangshuai.app
包及其全部子包,若是service或dao所在包不在io.github.gefangshuai.app
及其子包下,則不會被掃描!java

解決:@ComponentScan註解進行指定要掃描的包以及要掃描的類。git

參考:http://blog.csdn.net/u014695188/article/details/52263903github

相關文章
相關標籤/搜索