【问题标题】:Invalid bean definition with name 'configServicePropertySource'名称为“configServicePropertySource”的 bean 定义无效
【发布时间】:2019-04-08 00:19:59
【问题描述】:

我已经定义了ConfigServicePropertySourceLocator bean,如下所示

@Primary
@Bean
public ConfigServicePropertySourceLocator configServicePropertySource(
        ConfigClientProperties configClientProperties) {
    ConfigServicePropertySourceLocator sourceLocator = new ConfigServicePropertySourceLocator(
            configClientProperties);
    sourceLocator.setRestTemplate(clientOnlyRestTemplate());
    return sourceLocator;
}

但我得到以下异常(这是它在 docker 中的打印方式)虽然我的 bean 被标记为 @Primary

WARN 1 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
Invalid bean definition with name 'configServicePropertySource' defined in de.ig.client.security.configuration.ConfigClientSecurityConfiguration: 
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; 
dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=configClientSecurityConfiguration; 
factoryMethodName=configServicePropertySource; initMethodName=null; destroyMethodName=(inferred); 
defined in de.ig.client.security.configuration.ConfigClientSecurityConfiguration] for bean 'configServicePropertySource': 
There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; 
autowireCandidate=true; primary=false; factoryBeanName=configServiceBootstrapConfiguration; 
factoryMethodName=configServicePropertySource; initMethodName=null; destroyMethodName=(inferred); 
defined in org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration] bound.

【问题讨论】:

  • 你能查一下this
  • @buræquete 链接提到类加载问题。这是bean覆盖问题。更糟糕的是,即使使用 Primary 注释,它也会抛出异常。很奇怪。
  • 看起来很奇怪.. 但您可以添加@Order(-1) 并删除@Primary...
  • @JonathanJohx 下单后出现同样的异常。

标签: spring spring-boot spring-oauth2 spring-config


【解决方案1】:

通过设置解决了

spring.main.allow-bean-definition-overridingtrue

从引导版本 2.1 开始的新功能

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 1970-01-01
    • 2018-12-15
    • 2012-04-24
    • 1970-01-01
    • 2019-05-12
    • 2018-04-25
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多