天天看點

The bean 'dataSource', defined in BeanDefinition defined in class path resource

SpringCloud,SpringBoot,Mybatis整合時,啟動項目,遇到一個錯誤:

2019-02-13 15:38:02.393  INFO 7512 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-13 15:38:02.395 ERROR 7512 --- [           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$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:14424', transport: 'socket'

Process finished with exit code 1           

複制

排查後發現是由于SpringCloud,SpringBoot的版本對應有問題,我原本的版本如下:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.java4all</groupId>
	<artifactId>user-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>user-server</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>           

複制

springboot是2.1.2,springcloud是Finchley.SR2,這種對應是有問題的。後來在https://start.spring.io上生成時,當選擇springboot版本為2.1.2時,官方建立的項目springcloud版本為Greenwich.RELEASE。建議建立項目時,使用此網站來建立,官方來比對對應的依賴版本。