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

2年前 (2022) 程序员胖胖胖虎阿
177 0 0

报错信息:

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核心配置文件中常用的标签配置。

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

版权声明:程序员胖胖胖虎阿 发表于 2022年10月9日 下午4:16。
转载请注明:【Spring常见错误】No qualifying bean of type | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...