当前位置:网站首页>No qualifying bean of type问题解决
No qualifying bean of type问题解决
2022-07-30 16:24:00 【m0_67392661】
No qualifying bean of type问题解决
前言
No qualifying bean of type ‘xxx.xxx.xxx’ available: expected single matching bean but found 2:xxx.xxx.xxx
对于一个java开发新手来说, 应该经常会遇到这个问题,而且还不好意思询问周围同事,不要怕,今天就让我们解决掉它。
问题及解决方案
问题1
包扫描不正确或未扫描
- 默认情况下,如果使用了
springboot
,他会默认扫描启动类(使用了注解@SpringBootApplication)所在的包及子包,扫描的包中是否包含当前类所在的包。 - 检查
scanBasePackages
扫描路径是否包含当前类所在的包。
解决方案
将当前类所在的包加入扫描路径即可,例如当前类所在包为
com.demo1
@SpringBootApplication(@SpringBootApplication(scanBasePackages = {"com.demo","com.demo1"}))
切记不要忘了扫描当前启动类所在的包 ,否则又会导致其他类不能注入。
问题2
当前类是否注入了spring环境
- 当前类是否有 @Service @Component等注解
- 当前类是否被@Bean初始化
解决方案
增加对应注解将类注入到spring环境即可,例如:
@Service public class ServiceImpl1 implements Service{} 或 @Component public class ServiceImpl1 implements Service{} 或 @Bean public ServiceImpl serviceImpl(){ return new ServiceImpl(); }
使用@Bean方法所在的类也必须是要注入到spring环境中的。
问题3
多个类实现了同一个接口
查看当前接口是否被多个类实现
多个实现类是否未设置BeanName或者设置了相同的BeanName,例如:
@Service public class ServiceImpl1 implements Service{} @Service public class ServiceImpl2 implements Service{} 或 @Service("Service1") public class ServiceImpl3 implements Service{} @Service("Service1") public class ServiceImpl4 implements Service{}
以上方式都会出现异常。
注入Bean时是否指定了设置的BeanName,例如:
@Autowired private Service service;
解决方案
当我们需要多个类实现同一个接口时,需要为每个实现类配置BeanName,例如:
@Service("serviceImpl3")
public class ServiceImpl3 implements Service{}
@Service("serviceImpl2")
public class ServiceImpl2 implements Service{}
@Service("serviceImpl1")
public class ServiceImpl1 implements Service{}
建议首字母小写。
以上没有问题,那么使用的时候还需要指定BeanName,例如:
@Qualifier(value = "serviceImpl1")
@Autowired
private Service service;
关于@Qualifier使用,详见:Spring @Qualifier 注释
如果不想在每个使用处设置@Qualifier,只想在特殊场景下设置,那么只需要在默认的实现类上增加注解@Primary,例如:
@Primary
@Service
public class ServiceImpl implements Service{}
那么在使用的时候就可以直接注入了,默认会使用ServiceImpl,例如:
@Autowired
private Service service;
注意事项
当存在多个相同类型的Bean,强烈建议使用@Primary
把其中某一个Bean标识为“主要的”,使用@Autowired
注入时会首先使用被标记为@Primary
的Bean。
边栏推荐
- Minio 入门
- 你是一流的输家,你因此成为一流的赢家
- 华为云数据治理生产线DataArts,让“数据‘慧’说话”
- 字符串加千分位符与递归数组求和
- The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.
- Leetcode 119. Yang Hui's Triangle II
- Goland opens file saving and automatically formats
- SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
- Redis 复习计划 - Redis 数据结构和持久化机制
- Image information extraction DEM
猜你喜欢
(一)云计算技术学习--虚拟化vSphere学习
onenote use
huato hot update environment construction (DLL method hot update C# code)
You are a first-class loser, you become a first-class winner
Goland 开启文件保存自动进行格式化
服务器装好系统的电脑怎么分区
Login Module Debugging - Getting Started with Software Debugging
大厂面试官眼中的好简历到底长啥样
云风:不加班、不炫技,把复杂的问题简单化
安全业务收入增速超70% 三六零筑牢数字安全龙头
随机推荐
测试管理与规范
基于STM32F407使用ADC采集电压实验
Redis 复习计划 - Redis 数据结构和持久化机制
HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
[flutter] What is MaterialApp and Material design
PyQt5快速开发与实战 9.2 数据库处理
Visual Studio 集成Qt开发环境的一些注意事项
hcip--ospf综合实验
详解最实用的几种dll注入方式
Qt 动态库与静态库
3D激光SLAM:LeGO-LOAM论文解读---特征提取部分
【SOC】Classic output hello world
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
服务器装好系统的电脑怎么分区
【SOC FPGA】Peripheral KEY LED
安全业务收入增速超70% 三六零筑牢数字安全龙头
云风:不加班、不炫技,把复杂的问题简单化
SocialFi 何以成就 Web3 去中心化社交未来
Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法
为人处世之道,与君共勉!