Skip to content

Commit 70f3b0e

Browse files
committed
Only register DynamicPropertySourceBeanInitializer if necessary
This commit makes sure to check first if DynamicPropertySourceBeanInitializer has been registered before trying to register it. When running with AOT optimizations, the bean definition has been contributed so there is no need to register it again when the context customizer runs. Closes gh-33272
1 parent 2596e29 commit 70f3b0e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
7878
new DefaultDynamicPropertyRegistry(environment, this.methods.isEmpty());
7979
beanFactory.registerSingleton(DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME, dynamicPropertyRegistry);
8080

81-
BeanDefinition beanDefinition = new RootBeanDefinition(DynamicPropertySourceBeanInitializer.class);
82-
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
83-
beanDefinitionRegistry.registerBeanDefinition(
84-
DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME, beanDefinition);
81+
if (!beanDefinitionRegistry.containsBeanDefinition(DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME)) {
82+
BeanDefinition beanDefinition = new RootBeanDefinition(DynamicPropertySourceBeanInitializer.class);
83+
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
84+
beanDefinitionRegistry.registerBeanDefinition(
85+
DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME, beanDefinition);
86+
}
8587

8688
if (!this.methods.isEmpty()) {
8789
MutablePropertySources propertySources = environment.getPropertySources();

0 commit comments

Comments
 (0)