Skip to content

Commit c59e9a2

Browse files
committed
DynamicPropertyDefinitionRegistrar registers single Bean
Closes gh-70
1 parent fc2240b commit c59e9a2

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

spring-boot-testjars/src/main/java/org/springframework/experimental/boot/test/context/DynamicPropertyDefinitionRegistrar.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
class DynamicPropertyDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
4545

46+
public static final String REGISTRAR_BEAN_NAME = "testjarsDynamicPropertyRegistryPropertyRegistrar";
47+
4648
private final DynamicPropertyRegistryPropertyFactory registryPropertyFactory;
4749

4850
private final BeanFactory beanFactory;
@@ -57,6 +59,9 @@ class DynamicPropertyDefinitionRegistrar implements ImportBeanDefinitionRegistra
5759

5860
@Override
5961
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
62+
if (registry.containsBeanDefinition(REGISTRAR_BEAN_NAME)) {
63+
return;
64+
}
6065
if (this.beanFactory instanceof ConfigurableListableBeanFactory listableBeanFactory) {
6166
if (ClassUtils.isPresent("org.springframework.test.context.DynamicPropertyRegistrar",
6267
getClass().getClassLoader())) {
@@ -85,8 +90,7 @@ private void registerDynamicPropertyRegistrar(ConfigurableListableBeanFactory be
8590
BeanDefinitionBuilder registrarBdb = BeanDefinitionBuilder
8691
.rootBeanDefinition(DynamicPropertyRegistryPropertyRegistrar.class);
8792
registrarBdb.addConstructorArgValue(properties);
88-
registry.registerBeanDefinition("testjarsDynamicPropertyRegistryPropertyRegistrar",
89-
registrarBdb.getBeanDefinition());
93+
registry.registerBeanDefinition(REGISTRAR_BEAN_NAME, registrarBdb.getBeanDefinition());
9094
}
9195

9296
private void registerTestcontainersPropertySource(ConfigurableListableBeanFactory beanFactory,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.experimental.boot.test.context;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
/**
25+
* @author Rob Winch
26+
*/
27+
class DynamicPropertyDefinitionRegistrarTests {
28+
29+
@Test
30+
void importTwiceAndAllowBeanOverridingFalseThenNoException() {
31+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
32+
context.register(ImportTwiceTestConfiguration.class);
33+
context.setAllowBeanDefinitionOverriding(false);
34+
context.refresh();
35+
}
36+
}
37+
38+
@Configuration
39+
static class ImportTwiceTestConfiguration {
40+
41+
@EnableDynamicProperty
42+
@Configuration
43+
static class Config1 {
44+
45+
}
46+
47+
@EnableDynamicProperty
48+
@Configuration
49+
static class Config2 {
50+
51+
}
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)