Java Spring Boot 2.1 中的数据源 bean 覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53104697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataSource bean overriding in spring boot 2.1
提问by Mariusz Miesiak
I have upgraded to spring boot 2.1 release and I have got strange exception when starting up the application.
我已升级到 spring boot 2.1 版本,但在启动应用程序时出现了奇怪的异常。
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.
在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义的 BeanDefinition 中定义的 bean 'dataSource' 无法注册。具有该名称的 bean 已经在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] 中定义并且覆盖被禁用。
The full error message is:
完整的错误信息是:
[o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'dataSource' defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Cannot register bean definition [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] for bean 'dataSource': There is already [Root bean: class [null]; scope=refresh; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] bound.
[osbwscAnnotationConfigServletWebServerApplicationContext] 上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.support.BeanDefinitionOverrideException:在类路径资源 [org/springframework/boot/autoconfigure/ 中定义的 BeanDefinition 中定义了名称为“dataSource”的无效 bean 定义jdbc/DataSourceConfiguration$Hikari.class]: 无法注册 bean 定义 [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; 范围=; 抽象=假;懒惰初始化=假;autowireMode=0; 依赖检查=0;autowireCandidate=真;主要=假;factoryBeanName=null; 工厂方法名=空;initMethodName=null; destroyMethodName=null; 在 BeanDefinition 中定义的类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] 中为 bean 'dataSource' 定义:已经存在 [Root bean: class [null]; 范围=刷新;抽象=假;懒惰初始化=假;autowireMode=3; 依赖检查=0;autowireCandidate=false; 主要=假;factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; 工厂方法名=数据源;initMethodName=null; destroyMethodName=(推断);在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] 绑定中定义。springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; 工厂方法名=数据源;initMethodName=null; destroyMethodName=(推断);在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] 绑定中定义。springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; 工厂方法名=数据源;initMethodName=null; destroyMethodName=(推断);在类路径资源 [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] 绑定中定义。
Beans must not be overridden according to our policy and it's disabled with:
根据我们的政策,Bean 不得被覆盖,并且它被禁用:
spring.main.allow-bean-definition-overriding=false
I don't have any data source configuration in my application code. The only option that triggers this error is @EnableAutoConfiguration
and in my application properties I have set the data source type to:
我的应用程序代码中没有任何数据源配置。触发此错误的唯一选项是@EnableAutoConfiguration
,在我的应用程序属性中,我已将数据源类型设置为:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
The boot application is initialized with
启动应用程序初始化为
@SpringBootApplication
@EnableAutoConfiguration
public class MyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
new MyApplication()
.configure(new SpringApplicationBuilder(MyApplication.class))
.run(args);
}
}
There is also configuration class that imports various other configurations:
还有一个配置类可以导入各种其他配置:
@Configuration
@ImportResource(locations = {
"classpath*:conf/spring/*.xml",
"classpath*:conf/spring/core/*.xml",
"classpath*:conf/spring/plugin/**/*.xml"
})
@EnableAsync
@EnableRetry
@EnableCaching
@EnableBatchProcessing
@EnableCircuitBreaker
public class AppConfig {
...
}
Does anyone knows what could cause that issue and where to search?
有谁知道可能导致该问题的原因以及在哪里搜索?
It didn't happened prior to Spring Boot 2.1 (i.e. 2.0.5).
它没有发生在 Spring Boot 2.1(即 2.0.5)之前。
采纳答案by Brad Fontaine
I ran into a similar problem with this today and the following spring cloud config issue helped me: Issue 1142.
我今天遇到了类似的问题,以下 spring 云配置问题帮助了我: 问题 1142。
We were using Spring Cloud Config which is not compatible with Spring Boot 2.1.0 as of yet. The Greenwich release train of Spring Cloud will be compatible with Spring Boot 2.1.0.
我们使用的 Spring Cloud Config 目前与 Spring Boot 2.1.0 不兼容。Spring Cloud 的 Greenwich 发布系列将与 Spring Boot 2.1.0 兼容。
Your @EnableCircuitBreaker
annotation leads me to believe you might also be using a version of Spring Cloud that is not compatible with the 2.1.0 release of Spring Boot.
您的@EnableCircuitBreaker
注释让我相信您可能还在使用与 Spring Boot 2.1.0 版本不兼容的 Spring Cloud 版本。
回答by f.kowal
org.springframework.cloud:spring-cloud-context:2.0.2.RELEASE
org.springframework.cloud:spring-cloud-context:2.0.2.RELEASE
RefreshAutoConfigurationcauses this issue when refreshing HikariDatasource in
RefreshAutoConfiguration在刷新 HikariDatasource 时导致此问题
a possible fix is
一个可能的解决方法是
spring.cloud.refresh.enabled = false
回答by Kamal A. SIddiqui
For Spring Boot 2+ you can exclude auto-configuration
对于 Spring Boot 2+,您可以排除自动配置
@SpringBootApplication(exclude = ElasticsearchDataAutoConfiguration.class)
public class YourApplication {
... }
回答by Pulkownik
I ran into a similar problem and it is very generic (sometimes there are duplicated annotation sometimes duplicated beans). In case when you have duplicated annotation like @EnableJpaRepository the error message not mention this annotation at all. The best way to find where the problem is:
我遇到了一个类似的问题,它非常通用(有时有重复的注释,有时是重复的 bean)。如果您复制了@EnableJpaRepository 之类的注释,则错误消息根本不会提及此注释。找出问题所在的最佳方法:
Open class DefaultListableBeanFactory There should be code like this:
打开类 DefaultListableBeanFactory 应该有这样的代码:
BeanDefinition existingDefinition = (BeanDefinition)this.beanDefinitionMap.get(beanName);
if (existingDefinition != null) {
if (!this.isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
}
Put a breakpoint in line with throw new. Then existingDefinition.source.className is pointing to configuration which is already registered and here is the problem. When you check the beanDefinition.source.className you will compare both classes and find where duplicated code or annotation is, just delete/fix them.
将断点与 throw new 对齐。然后 existingDefinition.source.className 指向已经注册的配置,这就是问题所在。当您检查 beanDefinition.source.className 时,您将比较两个类并找到重复代码或注释的位置,只需删除/修复它们即可。
回答by Rahul
Adding following Property in application.properties would solve the issue .
在 application.properties 中添加以下属性将解决问题。
spring.main.allow-bean-definition-overriding=true
spring.main.allow-bean-definition-overriding=true
Further one might have to add another property to resolve another issue mentioned : https://github.com/openzipkin/zipkin/issues/2043
此外,可能需要添加另一个属性来解决提到的另一个问题:https: //github.com/openzipkin/zipkin/issues/2043
management.metrics.web.server.auto-time-requests=false
management.metrics.web.server.auto-time-requests=false
回答by Vicky
I got this issue when I try to configure Redis in my project, finally I resolve it by using:
当我尝试在我的项目中配置 Redis 时遇到了这个问题,最后我使用以下方法解决了它:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
I use SpringBoot 2.4.2
我使用 SpringBoot 2.4.2