Spring boot 不允许Bean定义重写?

Spring boot 不允许Bean定义重写?,spring-boot,Spring Boot,我想禁止在SpringApplication中重写bean定义。换句话说,我想要调用GenericApplicationContext.setAllowBeanDefinitionOverriding(false)的效果。在Spring Boot中有推荐的方法吗 好吧,您可以通过一些引导功能和上下文自定义来实现: @EnableAutoConfiguration 公共类MyApp{ 公共静态void main(字符串[]args)引发InterruptedException{ Configura

我想禁止在
SpringApplication
中重写bean定义。换句话说,我想要调用
GenericApplicationContext.setAllowBeanDefinitionOverriding(false)
的效果。在Spring Boot中有推荐的方法吗

好吧,您可以通过一些引导功能和上下文自定义来实现:

@EnableAutoConfiguration
公共类MyApp{
公共静态void main(字符串[]args)引发InterruptedException{
ConfigurableApplicationContext ctx=新的SpringApplicationBuilder(MyApp.class)
.initializers(新的ApplicationContextInitializer(){
@凌驾
公共void初始化(GenericaApplicationContext应用上下文){
applicationContext.setAllowBeanDefinitionOverriding(false);
}
}).run(args);
}
}

您可以从上述类的源代码和JavaDocs中找到所有其他信息。

我遇到过类似的问题,但不是在spring boot应用程序中,而是在具有基于xml的配置的普通spring mvc应用程序中,因此此解决方案可能会帮助人们寻找spring boot应用程序以外的其他应用程序

DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    beanFactory.setAllowBeanDefinitionOverriding(false);
    new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(new ClassPathResource("spring-first.xml"));

你试过什么吗?