報錯信息:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ssmpdemo.ServiceTest': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssmpdemo.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssmpdemo.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:?
?
對癥下藥:
很明顯,錯誤原因為SpringBoot自動注入Bean找不到相對應(yīng)的Bean類型。導(dǎo)致自動注入失敗。
既然發(fā)現(xiàn)錯誤就對癥下藥,找對應(yīng)的Bean,這里找的是Service對應(yīng)的實現(xiàn)類
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ssmpdemo.entity.User; import com.ssmpdemo.mapper.UserMapper; import com.ssmpdemo.service.UserService; public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
發(fā)現(xiàn)對應(yīng)的實現(xiàn)類開頭未加注解:(如果注解已加,可在相關(guān)問題找找是否有對應(yīng)的問題)
問題解決:
@Controller、@Service、@Repository、 @Component 加上其中一個。
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ssmpdemo.entity.User; import com.ssmpdemo.mapper.UserMapper; import com.ssmpdemo.service.UserService; import org.springframework.stereotype.Repository; @Repository public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
相關(guān)問題:
包目錄不對應(yīng)
例如:Application所在包:com.spring.demo
這時Spring只會掃描demo目錄下的所有文件,如果需要的Bean不在此目錄下,自然注入失敗。
只需將Bean移動到Application所在目錄下即可。
????作者主頁:??求不脫發(fā)的博客
???? 精選專欄:??SSM直擊大廠
???? 精彩摘要:MyBatis 核心配置文件(xxxConfig.xml),該文件配置了MyBatis的一些全局信息,,包含數(shù)據(jù)庫連接信息和MyBatis運(yùn)行時所需的各種特性,以及設(shè)置和響應(yīng)MyBatis行為的一些屬性。本文將深入淺出的介紹MyBatis核心配置文件中常用的標(biāo)簽配置。文章來源:http://www.zghlxwxcb.cn/news/detail-403983.html
????覺得文章還不錯的話歡迎大家點贊???收藏???評論??支持博主??文章來源地址http://www.zghlxwxcb.cn/news/detail-403983.html
到了這里,關(guān)于【Spring常見錯誤】No qualifying bean of type的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!