SpringBoot多数据源unsatisfied dependency expressed through method 'entityManagerFactoryBuilder...
本来的项目(基于SpringBoot 2.0.5-RELEASE)集成了JPA、mybatis的注解、XML方式访问DB。
后面集成多数据源的时候启动SpringBoot时出现了如下错误:
ERROR 32320 — [main] com.zaxxer.hikari.HikariConfig : HikariPool-1 - jdbcUrl is required with driverClassName.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method ‘entityManagerFactory’ parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘entityManagerFactoryBuilder’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method ‘entityManagerFactoryBuilder’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jpaVendorAdapter’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method ‘jpaVendorAdapter’ threw exception; nested exception is java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:474)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1096)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:535)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
… 24 more
后来查看了源码,流程如下:
@Bean(name = "test1DataSource")@ConfigurationProperties(prefix = "spring.datasource")@Primarypublic DataSource testDataSource() {return DataSourceBuilder.create().build();}
进入DataSourceBuilder.create().build():
public T build() {Class<? extends DataSource> type = getType();DataSource result = BeanUtils.instantiateClass(type);maybeGetDriverClassName();bind(result);return (T) result;}
maybeGetDriverClassName(); 方法:
private void maybeGetDriverClassName() {if (!this.properties.containsKey("driverClassName")&& this.properties.containsKey("url")) {String url = this.properties.get("url");String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();this.properties.put("driverClassName", driverClass);}}
bind(result) 方法:
private void bind(DataSource result) {ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties);ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();aliases.addAliases("url", "jdbc-url");aliases.addAliases("username", "user");Binder binder = new Binder(source.withAliases(aliases));binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));}
因为springboot首推的连接池使用了HikariCP连接池,GET到这个点, HikariDataSource的获取数据库连接的getConnection方法:
@Overridepublic Connection getConnection() throws SQLException{if (isClosed()) {throw new SQLException("HikariDataSource " + this + " has been closed.");}if (fastPathPool != null) {return fastPathPool.getConnection();}// See http://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_JavaHikariPool result = pool;if (result == null) {synchronized (this) {result = pool;if (result == null) {validate();LOGGER.info("{} - Starting...", getPoolName());try {pool = result = new HikariPool(this);this.seal();}catch (PoolInitializationException pie) {if (pie.getCause() instanceof SQLException) {throw (SQLException) pie.getCause();}else {throw pie;}}LOGGER.info("{} - Start completed.", getPoolName());}}}return result.getConnection();}
HikariConfig的validate方法
public void validate(){if (poolName == null) {poolName = generatePoolName();}else if (isRegisterMbeans && poolName.contains(":")) {throw new IllegalArgumentException("poolName cannot contain ':' when used with JMX");}// treat empty property as nullcatalog = getNullIfEmpty(catalog);connectionInitSql = getNullIfEmpty(connectionInitSql);connectionTestQuery = getNullIfEmpty(connectionTestQuery);transactionIsolationName = getNullIfEmpty(transactionIsolationName);dataSourceClassName = getNullIfEmpty(dataSourceClassName);dataSourceJndiName = getNullIfEmpty(dataSourceJndiName);driverClassName = getNullIfEmpty(driverClassName);jdbcUrl = getNullIfEmpty(jdbcUrl);// Check Data Source Optionsif (dataSource != null) {if (dataSourceClassName != null) {LOGGER.warn("{} - using dataSource and ignoring dataSourceClassName.", poolName);}}else if (dataSourceClassName != null) {if (driverClassName != null) {LOGGER.error("{} - cannot use driverClassName and dataSourceClassName together.", poolName);// NOTE: This exception text is referenced by a Spring Boot FailureAnalyzer, it should not be// changed without first notifying the Spring Boot developers.throw new IllegalStateException("cannot use driverClassName and dataSourceClassName together.");}else if (jdbcUrl != null) {LOGGER.warn("{} - using dataSourceClassName and ignoring jdbcUrl.", poolName);}}else if (jdbcUrl != null || dataSourceJndiName != null) {// ok}else if (driverClassName != null) {LOGGER.error("{} - jdbcUrl is required with driverClassName.", poolName);throw new IllegalArgumentException("jdbcUrl is required with driverClassName.");}else {LOGGER.error("{} - dataSource or dataSourceClassName or jdbcUrl is required.", poolName);throw new IllegalArgumentException("dataSource or dataSourceClassName or jdbcUrl is required.");}validateNumerics();if (LOGGER.isDebugEnabled() || unitTest) {logConfiguration();}}
这个if分支:
jdbcUrl = getNullIfEmpty(jdbcUrl);else if (driverClassName != null) {LOGGER.error("{} - jdbcUrl is required with driverClassName.", poolName);throw new IllegalArgumentException("jdbcUrl is required with driverClassName.");}
这里要求DataSource的参数存在 jdbcUrl 、以及driverClassName,而我们的配置是:
spring.datasource.url=jdbc:mysql://localhost:3306/mypydb
spring.datasource.username=root
spring.datasource.password=11111111
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource2.url=jdbc:mysql://localhost:3306/mypydb2
spring.datasource2.username=root
spring.datasource2.password=11111111
spring.datasource2.driver-class-name=com.mysql.jdbc.Driver
所以将url改为 jdbc-url 即可。
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/mypydbspring.datasource2.jdbc-url=jdbc:mysql://localhost:3306/mypydb2
SpringBoot多数据源unsatisfied dependency expressed through method 'entityManagerFactoryBuilder...相关推荐
- 【Shiro】Unsatisfied dependency expressed through method 'securityManager' parameter 3
大家好,我是烤鸭: 采坑记录,springboot 整合 shiro. 环境: springboot 2.0.5.RELEASE shiro-spring 1.4.0 shiro-redi ...
- Unsatisfied dependency expressed through method ‘redisTemplate‘ parameter 0;
原因分析: Error starting ApplicationContext. To display the auto-configuration report re-run your applic ...
- SpringBoot报错Unsatisfied dependency expressed through field userMapper和Whitelabel Error Page解决方案
搜索下面的错误信息得到解决方法 Unsatisfied dependency expressed through field 'userMapper' 项目结构如下 解决办法 在启动类中加入注解 里面 ...
- sping boot集成多数据源的时候会出现 unsatisfied dependency expressed through method
因为springboot首推的连接池使用了HikariCP连接池,GET到这个点, HikariDataSource的获取数据库连接的getConnection方法 修改application.pro ...
- 关于SpringCloud报错Unsatisfied dependency expressed through field ‘propertySourceLocators的解决之一
这里写目录标题 1. 问题简述 2. 报错显示 3. 问题说明 1. 问题简述 今天给大家看一下,关于SpringCloud报错,Unsatisfied dependency expressed th ...
- springBoot 启动之后报错Unsatisfied dependency expressed through field ‘baseMapper’
springBoot 启动之后报错Unsatisfied dependency expressed through field 'baseMapper' 框架:springboot+mybatispl ...
- SpringBoot启动报错Error creating bean with name 'xx': Unsatisfied dependency expressed through field xx
错误如下: 2020-05-11 08:55:21.333 WARN 937516 --- [ main] ConfigServletWebServerApplicationContext : Exc ...
- Unsatisfied dependency expressed through field 'service'
启动SpringBoot项目报以下错误 Error starting ApplicationContext. To display the conditions report re-run your ...
- Unsatisfied dependency expressed through field 'baseMapper'
今天项目 springboot 1.* 升级到 2.0.4,以至于将 Mybatis-Plus 升级,springBoot 启动之后报错Unsatisfied dependency expressed ...
最新文章
- 2021清华大学年度人物候选 | 曹丰泽:我要证明,理想主义的路是走得通的
- JXJJOI2018_T2_tank
- shardingjdbc每月分表_shardingjdbc分库分表测试
- jsp中的basePath和path (绝对路径 相对路径)
- AI基础:机器学习简易入门
- linux添加video驱动,linux下video驱动源码位置
- android androidruntime java,java – Runtime.exec():在Android中重启?
- 拓扑一致体参数化的复杂模型的等几何分析计算重用
- web前端入门指南:来看看这位大佬的学习之路吧!
- DSP 基于 TMS320F2803x 的 I2C 上的 PMBus 的软件应用
- 浏览器FLASH禁用后无法播放rtmp流怎么办webrtc视频流直播浏览器无插件播放也支持rtmp拉转成webrtc输出
- CSDN博客模板调查问卷
- docker network详解、教程
- 迪文屏与单片机c语言范例,STM32与迪文屏通讯 DMA模式
- python字符串处理编程实例_Python字符串处理实例详解
- JS获取跨域的cookie实例
- spark sql 之 collect_set collect_list 后WrappedArray 取值
- 局域网访问电脑中VMware虚拟机
- 事业人员辞职后自己怎么样交社保,到退休年龄可正常领养老金?
- An abnormal horizontal ListView-like pile layout.
热门文章
- 低眉信手续续弹,说尽心中无限事——python分支结构
- python 大智慧股池_如何删除大智慧系统股票池以及运行自添加的股票池
- 在国内的现货白银生存要具备的心态
- github网页打不开
- 【百度领航团】小白零基础python学习笔记
- 旅游行业网站怎么搭建?
- 施乐服务器显示exited,富士施乐故障维修代码.docx
- 面对面的办公室——纪念艾伦•图灵百年诞辰 1912.6.23-2012.6.23
- 机器学习学习笔记-持续学习(Continual Learning/ Life-long Learning)
- python从入门到秃头_六星教育helen老师:零基础学习python,最好先掌握flask框架...