DataSource bean覆盖在spring boot 2.1中

我已升级到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.

完整的错误消息是:

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

不得根据我们的政策覆盖 beans 类,并且禁用 beans 类:

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

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

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);
    }
}

还有一个配置类可以导入各种其他配置:

@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 {
    ...
}

有谁知道什么可能导致该问题以及在哪里搜索?

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

回答(3)

3 years ago

我今天遇到了类似的问题,下面的 Spring 季 Cloud 配置问题帮助了我:Issue 1142 .

我们使用的Spring Cloud Config与Spring Boot 2.1.0不兼容 . Spring Cloud的Greenwich发布系列将与Spring Boot 2.1.0兼容 .

您的 @EnableCircuitBreaker 注释让我相信您可能也在使用与Spring Boot的2.1.0版本不兼容的Spring Cloud版本 .

3 years ago

  • Spring Boot 2.1.x 默认禁用此类bean的覆盖 .

  • 尝试将以下行添加到应用程序属性:

  • spring.main.allow-bean-definition-overriding=true

  • 这会重新启用以前的行为 .

3 years ago

org.springframework.cloud:spring-cloud-context:2.0.2.RELEASE

RefreshAutoConfiguration在刷新HikariDatasource时会导致此问题

可能的解决办法是

spring.cloud.refresh.enabled = false