【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<UserMapper, User> 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<UserMapper, User> implements UserService {
}

相关问题:

包目录不对应

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

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

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

 

🙊🙊作者主页:🔗求不脱发的博客

📔📔 精选专栏:🔗SSM直击大厂

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

💞💞觉得文章还不错的话欢迎大家点赞👍➕收藏⭐️➕评论💬支持博主🤞

 

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求不脱发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值