多个@bean无法通过@resource注入对应的bean(org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found )
一、异常
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: masterDataSource,slaveDataSource
二、场景分析
看异常提示这个类型”javax.sql.DataSource“的bean期待一个单例bean但是发现了2个:masterDataSource,slaveDataSource。看打印的堆栈:

如上,我们发现有个DataSourceInitializer.init方法追踪进去:

这里就给了我们一个灵感,DataSourceInitializer这个数据源初始化是spring boot自动配置类启动的。如下图

继续追踪异常:最终定位在DefaultListableBeanFactory.resolveNamedBean()中如下代码块:

如上图第一,第二个箭头分别取@Primary和@Priority2种注解注释的bean,只要存在,就可以获取bean并返回。
三、解决方案
1. 在其中一个bean上加@Primary,使得自动配置时不报错。
@ConfigurationProperties(prefix = "study.datasource.master")
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
}
2. 在启动类注解:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
多个@bean无法通过@resource注入对应的bean(org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found )的更多相关文章
- 深入Spring Boot:怎样排查expected single matching bean but found 2的异常
写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...
- Spring Boot 自定义配置文件异常"expected single matching bean but found 2"
运行环境:Spring Boot 2.5.0, IDEA 2020.3.2 异常详细信息: Injection of resource dependencies failed; nested exce ...
- expected single matching bean but found 2
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'acc ...
- spring依赖注入单元测试:expected single matching bean but found 2
异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Caused by: org.springframewor ...
- Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "" available: expected at least 1 bean which qualifies as autowire candidate
项目使用spring, mybatis.因为分了多个模块,所以会这个模块引用了其它模块的现在,结果使用Junit测试的时候发现有两个模块不能自动注入dao和service问题.解决后在此记录一下. 解 ...
- 添加事务后 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type available
今天遇到了一个奇怪的问题 在没添加事务之前 所有的代码都是好的 , 当我添加了事务之后, 代码报错 org.springframework.beans.factory.NoSuchBeanDef ...
- 多数据源报错 expected single matching bean but found 2: xxx,xxx
问题: expected single matching bean but found 2: xxx,xxx 原因:在 Spring 容器中配置了两个类型Bean,Spring 容器将无法确定到底要用 ...
- Spring 3.2 @Autowired异常:expected single matching bean but found 2
在使用Sping做单元测试时候,对RequestMappingHandlerAdapter(从处理器包装过来的适配器)进行自动装配, 发现报:expected single matching bean ...
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dx.service.ItemService] found for dependency
在整合ssm框架,测试service层的时候报错 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: ...
随机推荐
- 「 CODE[VS] P2853 」 方格游戏
题目大意 给定一张 $n\times n$ 的网格.每个格子上都有一个系数 $a$,先下 $A$ 和 $B$ 两人选择两条 $(1,1)\rightarrow (n,n)$ 路径.要求着两条路径不能相 ...
- linux基础常用语句--新手
查询:ls查看全部内容:ls -n删除:rm -rf 文件名创建目录: mkdir解压:rpm -验证是否安装:rpm -p 文件名rpm -ivh --nodeps 不需要前置条件的安装查询当前路径 ...
- ubuntu 通过ppa源安装mysql5.6
添加mysql5.6的源 sudo add-apt-repository -y ppa:ondrej/mysql-5.6 更新源 sudo apt-get update 安装mysql5.6 sudo ...
- Android jdbc连接mysql报错解决方案 (Communications link failure)
最近调试安卓连接mysql真是心态爆炸,快两天才搞出来.以下整理一些常见问题. 检查manifest文件里网络权限是否打开 检查数据库IP是否有问题(包括一些沙雕错误,比如是不是在ip首或尾多了个空格 ...
- django中配置允许跨域请求
对于django 安装django-cors-headers,详情请看官方文档 pip install django-cors-headers 配置settings.py文件 a.在INSTALLED ...
- 第2章 取得大家的支持 录播感悟(意外的Sprint)
关于<取得大家的支持>这个故事我看了三遍,做了计划,做了时间轴,因为之前有过第1章<知易行难>的沟通和磨合后,相信会顺利很多吧!可是却是意外不断的发生: 1.本人车钥匙掉停车场 ...
- 最小公倍数LCM
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入2个正整数A,B,求A与B的最小公倍数. Input 2个数A,B,中间用空格隔开.(1<= A,B <= ...
- N的阶乘 mod P
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %) 例如:n = 10, P = 11,10 ...
- 解决使用myeclipse电脑卡的问题
1. 原因:myeclipse会自动更新,因此会占用大量内存 2. 解决方法: (1)window->Perferences->General->Startup and Shutdo ...
- 模拟赛 Problem 2 不等数列(num.cpp/c/pas)
Problem 2 不等数列(num.cpp/c/pas) [题目描述] 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入“>”和“<”.问在所有排列中,有多少个排列恰好有 ...