From bcf4c32426f0f84b1f4c30a047d7138100dccee7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 4 May 2026 15:38:33 -0700 Subject: [PATCH] Call setAllowBeanDefinitionOverriding before initializer Update initialization order in `SpringApplication` to ensure `setAllowBeanDefinitionOverriding` is called before initializiers. This prevents an initializer from accidentially overriding a bean. Closes gh-50264 --- .../springframework/boot/SpringApplication.java | 14 +++++++------- .../boot/SpringApplicationTests.java | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 661edccbe3e..c5d5af28bae 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -383,6 +383,13 @@ public class SpringApplication { context.setEnvironment(environment); postProcessApplicationContext(context); addAotGeneratedInitializerIfNecessary(this.initializers); + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); + if (beanFactory instanceof AbstractAutowireCapableBeanFactory autowireCapableBeanFactory) { + autowireCapableBeanFactory.setAllowCircularReferences(this.properties.isAllowCircularReferences()); + if (beanFactory instanceof DefaultListableBeanFactory listableBeanFactory) { + listableBeanFactory.setAllowBeanDefinitionOverriding(this.properties.isAllowBeanDefinitionOverriding()); + } + } applyInitializers(context); listeners.contextPrepared(context); bootstrapContext.close(context); @@ -391,17 +398,10 @@ public class SpringApplication { logStartupProfileInfo(context); } // Add boot specific singleton beans - ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } - if (beanFactory instanceof AbstractAutowireCapableBeanFactory autowireCapableBeanFactory) { - autowireCapableBeanFactory.setAllowCircularReferences(this.properties.isAllowCircularReferences()); - if (beanFactory instanceof DefaultListableBeanFactory listableBeanFactory) { - listableBeanFactory.setAllowBeanDefinitionOverriding(this.properties.isAllowBeanDefinitionOverriding()); - } - } if (this.properties.isLazyInitialization()) { context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); } diff --git a/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index a6d80f3f159..d4ac0bf3224 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -49,6 +49,7 @@ import org.springframework.aot.AotDetector; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.BeanRegistrar; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.beans.factory.annotation.Autowired; @@ -100,6 +101,7 @@ import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.SimpleApplicationEventMulticaster; import org.springframework.context.event.SmartApplicationListener; import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -1139,6 +1141,21 @@ class SpringApplicationTests { .isThrownBy(() -> new SpringApplication(ExampleConfig.class, OverrideConfig.class).run()); } + @Test + void beanDefinitionOverridingIsAppliedToInitializer() { // gh-50264 + assertThatExceptionOfType(BeanDefinitionOverrideException.class).isThrownBy(() -> { + BeanRegistrar registrar = (registry, env) -> { + registry.registerBean("someBean", String.class); + registry.registerBean("someBean", String.class); + }; + ApplicationContextInitializer initializer = (context) -> context + .register(registrar); + SpringApplication application = new SpringApplication(Example.class); + application.setInitializers(List.of(initializer)); + application.run(); + }); + } + @Test void beanDefinitionOverridingCanBeEnabled() { assertThat(new SpringApplication(ExampleConfig.class, OverrideConfig.class)