繁体   English   中英

扩展父 class 时出现 BeanDefinitionOverrideException

[英]BeanDefinitionOverrideException while extending parent class

在我们的spring-boot服务之一中,我们有以下情况。 我们有两个配置类

@Configuration
public class Foo{

      @Bean
      public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
         log.debug("Initializing rabbitTemplate for sending messages to: {}", rabbitProperties.getHost());
         RabbitTemplate template = new RabbitTemplate(connectionFactory);
         template.setMessageConverter(messageConverter);
         return template;
     }

}

@Configuration
public class Bar extends Foo{

    // Something else in here, but nothing with same method name or return type like in parent
}

我们现在得到一个BeanDefinitionOverrideException或多或少说 Bean 被覆盖,我完全理解为什么。 我现在还可以设置属性spring.main.allow-bean-definition-overriding=true但如果有其他聪明的解决方案我会很感兴趣,在这种情况下,我什至不会在子 class 中覆盖它。

另一种方法是不扩展配置 class,而是导入它:

@Configuration
public class Foo{

    @Bean
    public String getString(){
        return "something";
    }
}

@Import(Foo.class)
@Configuration
public class Bar {

    // Something else in here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM