2

My Spring microservices prototype applications can't get started with the following messages:

***************************
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

The app dependencies are the followings:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'

implementation 'org.springframework.boot:spring-boot-starter-webflux'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.springframework.security:spring-security-test'
}

I assume that both data-jpa and h2 come with datasource. I had a similar combination before without this problem. I guess that the problem could be resolved by excluding datasource in one of the dependencies. After some online search, I haven't found how that works.

Any suggestions?

vic
  • 2,548
  • 9
  • 44
  • 74
  • Are you using Spring Boot 2.1 and incompatible version of Spring Cloud? See https://stackoverflow.com/questions/53104697/datasource-bean-overriding-in-spring-boot-2-1. – Karol Dowbecki May 10 '19 at 15:33
  • For me it was caused by using incorrect spring cloud version with my spring boot starter --> For Spring 2.2.4 moving from Spring cloud version Finchley.Release to Hoxton.Release solved it for me – Chetan Gowda Dec 29 '21 at 08:01

1 Answers1

1

You can add the following to your main class:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

Rob Scully
  • 744
  • 5
  • 10
  • That will solve the problem but I need to do a manual configuration for datasource. – vic May 10 '19 at 18:07
  • You’ll need to create a class using @Configuration annotation and create your DataSource bean there. Autowire your url, user, password and annotate the method with Bean and return your datasource. – Rob Scully May 11 '19 at 10:11