Spring Boot 2.1中的DataSource Bean覆盖 [英] DataSource bean overriding in spring boot 2.1

查看:650
本文介绍了Spring Boot 2.1中的DataSource Bean覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已升级到Spring Boot 2.1版本,并且在启动应用程序时遇到了奇怪的异常.

I have upgraded to spring boot 2.1 release and I have got strange exception when starting up the application.

无法注册在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]中定义的BeanDefinition中定义的bean'dataSource'.在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]中已经定义了具有该名称的Bean,并且禁止了覆盖.

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.

完整的错误消息是:

[osbwscAnnotationConfigServletWebServerApplicationContext]上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.support.BeanDefinitionOverrideException:在类路径资源[org/springframework/中定义的BeanDefinition中定义了名称为'dataSource'的无效bean定义. boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]:无法注册bean定义[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;在bean的数据源"的类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]中定义的BeanDefinition中定义:已经存在[Root bean:class [null];范围=刷新; 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 =(推断);在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration $ Hikari.class]中定义.

[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.

根据我们的政策,不得覆盖Bean,并通过以下方式禁用它:

Beans must not be overridden according to our policy and it's disabled with:

spring.main.allow-bean-definition-overriding=false

我的应用程序代码中没有任何数据源配置.触发此错误的唯一选项是@EnableAutoConfiguration,并且在我的应用程序属性中,我已将数据源类型设置为:

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:

spring.datasource.type=com.zaxxer.hikari.HikariDataSource

启动应用程序初始化为

@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?

在Spring Boot 2.1(即2.0.5)之前没有发生.

It didn't happened prior to Spring Boot 2.1 (i.e. 2.0.5).

推荐答案

我今天遇到了类似的问题,随后的春季云配置问题帮助了我:

I ran into a similar problem with this today and the following spring cloud config issue helped me: Issue 1142.

到目前为止,我们正在使用与Spring Boot 2.1.0不兼容的Spring Cloud Config. Spring Cloud的格林威治发布系列将与Spring Boot 2.1.0兼容.

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.

您的@EnableCircuitBreaker注释使我相信您可能还使用的Spring Cloud版本与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.

这篇关于Spring Boot 2.1中的DataSource Bean覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆