vic
vic

Reputation: 2818

How to resolve "The bean 'dataSource', defined in BeanDefinition defined in class path resource"?

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?

Upvotes: 2

Views: 3677

Answers (1)

Rob Scully
Rob Scully

Reputation: 790

You can add the following to your main class:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

Upvotes: 1

Related Questions