今天在启动springboot+springcloud项目的时候,发现报一个错误 如下错误

Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
2020-09-17 16:34:52.056 ERROR 19128 — [ main] o.s.b.d.LoggingFailureAnalysisReporter :

APPLICATION FAILED TO START

Description:

The bean ‘dataSource’, defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration H i k a r i . c l a s s ] , c o u l d n o t b e r e g i s t e r e d . A b e a n w i t h t h a t n a m e h a s a l r e a d y b e e n d e f i n e d i n c l a s s p a t h r e s o u r c e [ o r g / s p r i n g f r a m e w o r k / b o o t / a u t o c o n f i g u r e / j d b c / D a t a S o u r c e C o n f i g u r a t i o n Hikari.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Disconnected from the target VM, address: ‘127.0.0.1:62029’, transport: ‘socket’

Process finished with exit code 1

从错误信息中可以看到,dataSource这个bean已经被register了

从下图看:
在这里插入图片描述
只要在配置文件中加入

spring.main.allow-bean-definition-overriding=true

设置为true时,后定义的bean会覆盖之前定义的相同名称的bean,如图
在这里插入图片描述

根本原因分析

我们分析根本原因,发现上面解决方案不是根本原因,终究原因后面发现是SpringBoot和SpringCloud版本匹配问题导致的。

  • 问题前版本号为
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  • 解决后版本为
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

把版本降下来后,问题自然解决了,这才是推荐方案,从根本上解决

所以,我们在技术选型时,不用一味的追求最新的,要考虑到各组件的兼容性,选择最合适的,才是最好的。

最后提醒一下:

spring中默认是true,也就是默认支持名称相同的bean的覆盖。而springboot中的默认值是false,也就是不支持名称相同的bean被覆盖。

如果能帮到您,记得关注一下哈

前言今天在启动springboot+springcloud项目的时候,发现报一个错误 如下错误Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.2020-09-17 16:34:52.056 ERROR 19128 — [ main] o.s.b.d.LoggingFailureAnalysisReporte <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> Sharding-JDBC定位为轻量级Java框架,在Java的JDBC层提供的额外服务。 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。 使用Sharding-JDBC可以在程序中轻松的实现数据库读写分离。 2.特点: 1). 适用于任何基于JDBC的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。 2). 支持任何第三方的数据库连接
项目启动时报错,如下: Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-04-25 18:05:24.483 ERROR 7880 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-01-11 10:55:38.248 ERROR 12708 --- [ main] o.s.b...
原因是导入了jdbc的依赖,使用@Configuration注解向spring注入了dataSource bean。 但是因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。 有两个办法: 去除spring-boot-starter-jdbc的依赖或者mybatis的依赖 在SpringbootApplication.class里添加注解 @EnableAutoConfiguration(exclude={D.
Springboot报错Error creating bean with name ‘dataSourcedefined in class path resource解决方案
公司最近的项目开发中有一个数据库表,在给甲方部署试运行阶段发现有一个接口查询速度特别慢,然后经过排查发现,这个数据库表的的数据每天以6000-9000条的量激增,两个多月的时间已经60多万条。然后经过开会讨论,决定从两方面下手,一方面就是优化数据库结构,另一方面就是分库分表。虽然最终采用的是优化数据库的方式解决问题,在这之前,研究学习了大概一上午时间也简单实现了分库分表的方式解决问题,查询速度得到了明显的提高。在此做一个简单的记录。 2.技术路线 目前比...
什么是ShardingSphere Apache ShardingSphere 是一套开源的分布式数据库中间件解决方案组成的生态圈,它由 JDBC、Proxy 和 Sidecar(规划中)这 3 款相互独立,却又能够混合部署配合使用的产品组成。 它们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如 Java 同构、异构语言、云原生等各种多样化的应用场景。 Apache ShardingSphere 定位为关系型数据库中间件,旨在充分合理地在分布式的场景下利用关系型数据库的计算和存储能力,而并非
ShardingSphere官网:http://shardingsphere.apache.org 什么是 ShardingSphere ,它有什么好处?它能做什么事情?它有什么特点? Apache ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC、S...