查看“关于 hikari 多数据源配置的坑”的源代码
←
关于 hikari 多数据源配置的坑
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
application.yml<syntaxhighlight lang="yaml"> spring: datasource: hikari: datasource-world: jdbc-url: jdbc:mysql://127.0.0.1:3306/world username: root password: 123456 datasource-testdb: jdbc-url: jdbc:mysql://127.0.0.1:3306/testdb username: root password: 123456 </syntaxhighlight>DataSourceTestdbConfig.java<syntaxhighlight lang="java"> package io.github.jihch.config; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "io.github.jihch.mapper.testdb", sqlSessionTemplateRef = "sqlSessionTemplateTestdb") public class DataSourceTestdbConfig { @Bean("dataSourceTestdb") @ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-testdb") public DataSource dataSourceTestdb() { return new HikariDataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryTestdb(@Qualifier("dataSourceTestdb") HikariDataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateTestdb(@Qualifier("sqlSessionFactoryTestdb") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } </syntaxhighlight>DataSourceWorldConfig.java<syntaxhighlight lang="java"> package io.github.jihch.config; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "io.github.jihch.mapper.world", sqlSessionTemplateRef = "sqlSessionTemplateWorld") public class DataSourceWorldConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari.datasource-world") public DataSource dataSourceWorld() { return new HikariDataSource(); } @Bean public SqlSessionFactory sqlSessionFactoryWorld(@Qualifier("dataSourceWorld") HikariDataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean(); bean.setDataSource(dataSource); return bean.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplateWorld(@Qualifier("sqlSessionFactoryWorld") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } </syntaxhighlight>这套多数据源配置在启动的时候一直在抛异常<syntaxhighlight lang="console"> Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zaxxer.hikari.HikariDataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=dataSourceWorld)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790) ~[spring-beans-5.3.13.jar:5.3.13] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346) ~[spring-beans-5.3.13.jar:5.3.13] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.13.jar:5.3.13] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.13.jar:5.3.13] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.13.jar:5.3.13] ... 20 common frames omitted </syntaxhighlight>定位半天发现容器里 type=javax.sql.DataSource 的对象两个都在 dataSourceTestdb 和 dataSourceWorld; 但是 type=HikariDataSource 的对象就只有 dataSourceTestdb 一个,dataSourceWorld 没有算在内; 试了试把注入到 SqlSessionFactory 的 Bean 定义方法的入参类型改成 javax.sql.DataSource 就没问题了。
返回至
关于 hikari 多数据源配置的坑
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息