当前位置:网站首页>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。
边栏推荐
- go 学习03 基础知识变量类型
- Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
- Login Module Debugging - Getting Started with Software Debugging
- Horizontal Pod Autoscaler(HPA)
- Windows MySQL 安装配置
- Chapter 6: Decisive Autumn Moves
- 【HMS core】【FAQ】A collection of typical questions about Account, IAP, Location Kit and HarmonyOS 1
- 04、Activity的基本使用
- 配置Path环境变量
- PHP message feedback management system source code
猜你喜欢
随机推荐
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
SMI 与 Gateway API 的 GAMMA 倡议意味着什么?
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
vivo宣布延长产品保修期限 系统上线多种功能服务
C语言学习之旅 【函数(二)】
Nervegrowold d2l (7) kaggle housing forecast model, numerical stability and the initialization and activation function
全职做自媒体靠谱吗?
PHP message feedback management system source code
rhce笔记3
李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数
【SOC】经典输出hello world
安全业务收入增速超70% 三六零筑牢数字安全龙头
服务器装好系统的电脑怎么分区
【C语言】指针和数组的深入理解(第二期)
go 学习03 基础知识变量类型
Mirror stand to collect
(一)云计算技术学习--虚拟化vSphere学习
测试管理与规范
Discuz杂志/新闻报道模板(jeavi_line)UTF8-GBK模板
FME's scheme and operation process for reading and writing cass data









