【问题标题】:How to resolve "The bean 'dataSource', defined in BeanDefinition defined in class path resource"?如何解决“在类路径资源中定义的 BeanDefinition 中定义的 bean 'dataSource'”?
【发布时间】:2019-09-28 12:54:29
【问题描述】:

我的 Spring 微服务原型应用程序无法启动并显示以下消息:

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

应用依赖如下:

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'
}

我假设 data-jpa 和 h2 都带有数据源。我之前有过类似的组合,没有这个问题。我想可以通过在依赖项之一中排除数据源来解决问题。经过网上搜索,我没有找到它是如何工作的。

有什么建议吗?

【问题讨论】:

  • 您使用的是 Spring Boot 2.1 和不兼容的 Spring Cloud 版本吗?见stackoverflow.com/questions/53104697/…
  • 对我来说,这是因为我的 Spring Boot 启动器使用了不正确的 Spring Cloud 版本 --> 对于 Spring 2.2.4 从 Spring Cloud 版本 Finchley.Release 到 Hoxton.Release 为我解决了这个问题跨度>

标签: spring-boot spring-cloud


【解决方案1】:

您可以在主类中添加以下内容:

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

【讨论】:

  • 这样可以解决问题,但我需要对数据源进行手动配置。
  • 您需要使用@Configuration 注解创建一个类并在那里创建您的DataSource bean。自动连接您的 url、用户、密码并使用 Bean 注释该方法并返回您的数据源。
猜你喜欢
  • 2012-04-24
  • 2021-06-28
  • 2019-08-05
  • 2015-03-18
  • 2017-03-02
  • 2019-02-02
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
相关资源
最近更新 更多