栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring常见错误】No qualifying bean of type

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring常见错误】No qualifying bean of type

报错信息:

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找不到相对应的Bean类型。导致自动注入失败。

既然发现错误就对症下药,找对应的Bean,这里找的是Service对应的实现类

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 implements UserService {
}

发现对应的实现类开头未加注解:(如果注解已加,可在相关问题找找是否有对应的问题)

问题解决:

@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 implements UserService {
}
相关问题:

包目录不对应

例如:Application所在包:com.spring.demo

这时Spring只会扫描demo目录下的所有文件,如果需要的Bean不在此目录下,自然注入失败。

只需将Bean移动到Application所在目录下即可。

作者主页:求不脱发的博客

 精选专栏:SSM直击大厂

 精彩摘要:MyBatis 核心配置文件(xxxConfig.xml),该文件配置了MyBatis的一些全局信息,,包含数据库连接信息和MyBatis运行时所需的各种特性,以及设置和响应MyBatis行为的一些属性。本文将深入浅出的介绍MyBatis核心配置文件中常用的标签配置。

觉得文章还不错的话欢迎大家点赞➕收藏⭐️➕评论支持博主爛

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849958.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号