一、异常

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 )的更多相关文章

  1. 深入Spring Boot:怎样排查expected single matching bean but found 2的异常

    写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...

  2. 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 ...

  3. expected single matching bean but found 2

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'acc ...

  4. spring依赖注入单元测试:expected single matching bean but found 2

    异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Caused by: org.springframewor ...

  5. 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问题.解决后在此记录一下. 解 ...

  6. 添加事务后 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type available

    今天遇到了一个奇怪的问题   在没添加事务之前  所有的代码都是好的 , 当我添加了事务之后, 代码报错 org.springframework.beans.factory.NoSuchBeanDef ...

  7. 多数据源报错 expected single matching bean but found 2: xxx,xxx

    问题: expected single matching bean but found 2: xxx,xxx 原因:在 Spring 容器中配置了两个类型Bean,Spring 容器将无法确定到底要用 ...

  8. Spring 3.2 @Autowired异常:expected single matching bean but found 2

    在使用Sping做单元测试时候,对RequestMappingHandlerAdapter(从处理器包装过来的适配器)进行自动装配, 发现报:expected single matching bean ...

  9. 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: ...

随机推荐

  1. UVA - 10410 Tree Reconstruction(栈处理递归)

    题目: 给出一棵树的BFS和DFS序列,输出这棵树中每个结点的子结点列表.BFS和DFS序列在生成的时候,当一个结点被扩展时,其所有子结点应该按照编号从小 到大的顺序访问. 思路: 一开始是想根据BF ...

  2. springboot的jsp热部署

    使用springboot每次修改jsp都需要重新启动是不是很麻烦呢?以下是解决办法! yml格式 server: servlet: jsp: init-parameters: development: ...

  3. db2 group by的疑惑。

    按借据号分组,显示每组的条数:

  4. vue中路由

    关于每次点击链接都要刷新页面的问题众所周知,开发单页应用就是因为那丝般顺滑的体验效果,如果每次点击都会刷新页面… 出现这个的原因是因为使用了window.location来跳转,只需要使用使用rout ...

  5. Git学习总结(14)——Git使用前的注意事项

    连接方式https.ssh 在使用git的时候,不管你的服务器是开源平台github还是私服gitlab,你都需要clone仓库到本地,这个clone的时候就需要你选择连接方式.这个连接方式决定了你与 ...

  6. [K/3Cloud]调用动态表单时,传递自定义参数

    插件中在调用动态表单时,通过DynamicFormShowParameter的CustomParams,增加自定义的参数. private void ShowMaterialStock() { obj ...

  7. Docker website

    https://github.com/docker/labs/  (nguo123gmail  Cooooos123!) Docker Tutorials and Labs At this time ...

  8. SVN提交时报错:Commit blocked by pre-commit hook (exit code 1) with no output.

    可能的原因: 提交代码的SVN命令中,Comment长度短了.参考:http://tortoisesvn.net/docs/nightly/TortoiseSVN_en/tsvn-howto-minl ...

  9. linux 创建 bootable iso 文件

    windows制作iso文件通过ultraiso可以实现. linux下用mkisofs这个命令就可以 最简单的用法 mkisofs -o target.iso source 要制作可以启动的iso文 ...

  10. Javascript: 动态显示进度条

    {% if not config.exec_id == '' %} <br /> <div class="progress"> <div class= ...