Skip to content

Commit

Permalink
Make DynamicPropertyRegistrarBeanInitializer public
Browse files Browse the repository at this point in the history
In order to allow third parties (for example, Spring Boot) to ensure
that a DynamicPropertyRegistrarBeanInitializer has been registered in
an ApplicationContext which has not been customized by the
DynamicPropertiesContextCustomizer, this commit converts
DynamicPropertyRegistrarBeanInitializer to a public API for use in such
special use cases.

Closes gh-33593
  • Loading branch information
sbrannen committed Sep 26, 2024
1 parent b4bfb89 commit e55fe90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
*/
class DynamicPropertiesContextCustomizer implements ContextCustomizer {

private static final String DYNAMIC_PROPERTY_REGISTRAR_BEAN_INITIALIZER_BEAN_NAME =
DynamicPropertiesContextCustomizer.class.getName() + ".dynamicPropertyRegistrarBeanInitializer";


private final Set<Method> methods;


Expand All @@ -70,11 +66,10 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
throw new IllegalStateException("BeanFactory must be a BeanDefinitionRegistry");
}

if (!beanDefinitionRegistry.containsBeanDefinition(DYNAMIC_PROPERTY_REGISTRAR_BEAN_INITIALIZER_BEAN_NAME)) {
if (!beanDefinitionRegistry.containsBeanDefinition(DynamicPropertyRegistrarBeanInitializer.BEAN_NAME)) {
BeanDefinition beanDefinition = new RootBeanDefinition(DynamicPropertyRegistrarBeanInitializer.class);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
beanDefinitionRegistry.registerBeanDefinition(
DYNAMIC_PROPERTY_REGISTRAR_BEAN_INITIALIZER_BEAN_NAME, beanDefinition);
beanDefinitionRegistry.registerBeanDefinition(DynamicPropertyRegistrarBeanInitializer.BEAN_NAME, beanDefinition);
}

if (!this.methods.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@
import org.springframework.test.context.DynamicPropertyRegistry;

/**
* Internal component which eagerly initializes {@link DynamicPropertyRegistrar}
* {@link BeanFactoryInitializer} that eagerly initializes {@link DynamicPropertyRegistrar}
* beans.
*
* <p>Primarily intended for internal use within the Spring TestContext Framework.
*
* @author Sam Brannen
* @since 6.2
*/
class DynamicPropertyRegistrarBeanInitializer implements BeanFactoryInitializer<ListableBeanFactory>, EnvironmentAware {
public class DynamicPropertyRegistrarBeanInitializer implements BeanFactoryInitializer<ListableBeanFactory>, EnvironmentAware {

private static final Log logger = LogFactory.getLog(DynamicPropertyRegistrarBeanInitializer.class);

/**
* The bean name of the internally managed {@code DynamicPropertyRegistrarBeanInitializer}.
*/
static final String BEAN_NAME =
"org.springframework.test.context.support.internalDynamicPropertyRegistrarBeanInitializer";


@Nullable
private ConfigurableEnvironment environment;
Expand Down

0 comments on commit e55fe90

Please sign in to comment.