Register DynamicPropertyRegistry as singleton bean in test ApplicationContext
Prior to this commit, DynamicPropertyRegistry could only be used with a
static @DynamicPropertySource method in an integration test class;
however, it can also be useful to be able to register a "dynamic
property" from within a test's ApplicationContext -- for example, in a
@Bean method in a @Configuration class that is specific to testing
scenarios.
To support such use cases, this commit updates the dynamic property
source infrastructure so that a DynamicPropertyRegistry is always
registered as a singleton bean in a test's ApplicationContext. This
allows DynamicPropertyRegistry to be autowired into a @Configuration
class or supplied to a @Bean method as an argument as shown in the
following example.
@Bean
@DynamicPropertySource
ApiServer apiServer(DynamicPropertyRegistry registry) {
ApiServer apiServer = new ApiServer();
registry.add("api.url", apiServer::getUrl);
return apiServer;
}
Note that the use of @DynamicPropertySource on the @Bean method is
optional and results in the corresponding bean being eagerly
initialized so that other singleton beans in the context can be given
access to the dynamic properties sourced from that bean when those
other beans are initialized.
Side note: DynamicPropertySourceBeanInitializer temporarily implements
LoadTimeWeaverAware since doing so is currently the only way to have a
component eagerly initialized before the
ConfigurableListableBeanFactory.preInstantiateSingletons() phase.
However, we plan to introduce a first-class callback to support such
use cases in the future.
Closes gh-32271
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,9 +19,25 @@ package org.springframework.test.context;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Registry used with {@link DynamicPropertySource @DynamicPropertySource}
|
||||
* methods so that they can add properties to the {@code Environment} that have
|
||||
* dynamically resolved values.
|
||||
* Registry that is used to add properties with dynamically resolved values to
|
||||
* the {@code Environment}.
|
||||
*
|
||||
* <p>A {@code DynamicPropertyRegistry} is supplied as an argument to static
|
||||
* {@link DynamicPropertySource @DynamicPropertySource} methods in integration
|
||||
* test classes.
|
||||
*
|
||||
* <p>As of Spring Framework 6.2, a {@code DynamicPropertyRegistry} is also
|
||||
* registered as a singleton bean in the test's {@code ApplicationContext}. This
|
||||
* allows a {@code DynamicPropertyRegistry} to be autowired into a
|
||||
* {@code @Configuration} class or supplied to a {@code @Bean} method as an
|
||||
* argument, making it possible to register a dynamic property from within a test's
|
||||
* {@code ApplicationContext}. For example, a {@code @Bean} method can register
|
||||
* a property whose value is dynamically sourced from the bean that the method
|
||||
* returns. Note that such a {@code @Bean} method can optionally be annotated
|
||||
* with {@code @DynamicPropertySource} to enforce eager initialization of the
|
||||
* bean within the context, thereby ensuring that any dynamic properties sourced
|
||||
* from that bean are available to other singleton beans within the context.
|
||||
* See {@link DynamicPropertySource @DynamicPropertySource} for an example.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
|
||||
@@ -23,29 +23,43 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* {@code @DynamicPropertySource} is an annotation that can be applied to methods
|
||||
* in integration test classes that need to add properties with dynamic values to
|
||||
* the {@code Environment}'s set of {@code PropertySources}.
|
||||
* {@code @DynamicPropertySource} is an annotation that can be applied to static
|
||||
* methods in integration test classes or to {@code @Bean} methods in test
|
||||
* {@code @Configuration} classes in order to add properties with dynamic values
|
||||
* to the {@code Environment}'s set of {@code PropertySources}.
|
||||
*
|
||||
* <p>This annotation and its supporting infrastructure were originally designed
|
||||
* to allow properties from
|
||||
* <a href="https://www.testcontainers.org/">Testcontainers</a> based tests to be
|
||||
* exposed easily to Spring integration tests. However, this feature may also be
|
||||
* used with any form of external resource whose lifecycle is maintained outside
|
||||
* exposed easily to Spring integration tests. However, this feature may be used
|
||||
* with any form of external resource whose lifecycle is managed outside the
|
||||
* test's {@code ApplicationContext} or with beans whose lifecycle is managed by
|
||||
* the test's {@code ApplicationContext}.
|
||||
*
|
||||
* <p>Methods annotated with {@code @DynamicPropertySource} must be {@code static}
|
||||
* and must have a single {@link DynamicPropertyRegistry} argument which is used
|
||||
* to add <em>name-value</em> pairs to the {@code Environment}'s set of
|
||||
* {@code PropertySources}. Values are dynamic and provided via a
|
||||
* {@link java.util.function.Supplier} which is only invoked when the property
|
||||
* is resolved. Typically, method references are used to supply values, as in the
|
||||
* example below.
|
||||
* <p>{@code @DynamicPropertySource}-annotated methods use a
|
||||
* {@code DynamicPropertyRegistry} to add <em>name-value</em> pairs to the
|
||||
* {@code Environment}'s set of {@code PropertySources}. Values are dynamic and
|
||||
* provided via a {@link java.util.function.Supplier} which is only invoked when
|
||||
* the property is resolved. Typically, method references are used to supply values,
|
||||
* as in the example below.
|
||||
*
|
||||
* <p>As of Spring Framework 5.3.2, dynamic properties from methods annotated with
|
||||
* {@code @DynamicPropertySource} will be <em>inherited</em> from enclosing test
|
||||
* classes, analogous to inheritance from superclasses and interfaces. See
|
||||
* {@link NestedTestConfiguration @NestedTestConfiguration} for details.
|
||||
* <p>Methods in integration test classes that are annotated with
|
||||
* {@code @DynamicPropertySource} must be {@code static} and must accept a single
|
||||
* {@link DynamicPropertyRegistry} argument.
|
||||
*
|
||||
* <p>{@code @Bean} methods annotated with {@code @DynamicPropertySource} may
|
||||
* either accept an argument of type {@code DynamicPropertyRegistry} or access a
|
||||
* {@code DynamicPropertyRegistry} instance autowired into their enclosing
|
||||
* {@code @Configuration} class. Note, however, that {@code @Bean} methods which
|
||||
* interact with a {@code DynamicPropertyRegistry} are not required to be annotated
|
||||
* with {@code @DynamicPropertySource} unless they need to enforce eager
|
||||
* initialization of the bean within the context.
|
||||
* See {@link DynamicPropertyRegistry} for details.
|
||||
*
|
||||
* <p>Dynamic properties from methods annotated with {@code @DynamicPropertySource}
|
||||
* will be <em>inherited</em> from enclosing test classes, analogous to inheritance
|
||||
* from superclasses and interfaces.
|
||||
* See {@link NestedTestConfiguration @NestedTestConfiguration} for details.
|
||||
*
|
||||
* <p><strong>NOTE</strong>: if you use {@code @DynamicPropertySource} in a base
|
||||
* class and discover that tests in subclasses fail because the dynamic properties
|
||||
@@ -64,7 +78,13 @@ import java.lang.annotation.Target;
|
||||
* override properties loaded via {@code @TestPropertySource}, system property
|
||||
* sources, and application property sources.
|
||||
*
|
||||
* <h3>Example</h3>
|
||||
* <h3>Examples</h3>
|
||||
*
|
||||
* <p>The following example demonstrates how to use {@code @DynamicPropertySource}
|
||||
* in an integration test class. Beans in the {@code ApplicationContext} can
|
||||
* access the {@code redis.host} and {@code redis.port} properties which are
|
||||
* dynamically retrieved from the Redis container.
|
||||
*
|
||||
* <pre class="code">
|
||||
* @SpringJUnitConfig(...)
|
||||
* @Testcontainers
|
||||
@@ -81,7 +101,24 @@ import java.lang.annotation.Target;
|
||||
* registry.add("redis.host", redis::getHost);
|
||||
* registry.add("redis.port", redis::getFirstMappedPort);
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* <p>The following example demonstrates how to use {@code @DynamicPropertySource}
|
||||
* with a {@code @Bean} method. Beans in the {@code ApplicationContext} can
|
||||
* access the {@code api.url} property which is dynamically retrieved from the
|
||||
* {@code ApiServer} bean.
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
* class TestConfig {
|
||||
*
|
||||
* @Bean
|
||||
* @DynamicPropertySource
|
||||
* ApiServer apiServer(DynamicPropertyRegistry registry) {
|
||||
* ApiServer apiServer = new ApiServer();
|
||||
* registry.add("api.url", apiServer::getUrl);
|
||||
* return apiServer;
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,14 +18,15 @@ package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
@@ -35,8 +36,10 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizer} to support
|
||||
* {@link DynamicPropertySource @DynamicPropertySource} methods.
|
||||
* {@link ContextCustomizer} which supports
|
||||
* {@link DynamicPropertySource @DynamicPropertySource} methods and registers a
|
||||
* {@link DynamicPropertyRegistry} as a singleton bean in the container for use
|
||||
* in {@code @Configuration} classes and {@code @Bean} methods.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
@@ -45,7 +48,12 @@ import org.springframework.util.ReflectionUtils;
|
||||
*/
|
||||
class DynamicPropertiesContextCustomizer implements ContextCustomizer {
|
||||
|
||||
private static final String PROPERTY_SOURCE_NAME = "Dynamic Test Properties";
|
||||
private static final String DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME =
|
||||
DynamicPropertiesContextCustomizer.class.getName() + ".dynamicPropertyRegistry";
|
||||
|
||||
private static final String DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME =
|
||||
DynamicPropertiesContextCustomizer.class.getName() + "dynamicPropertySourceBeanInitializer";
|
||||
|
||||
|
||||
private final Set<Method> methods;
|
||||
|
||||
@@ -61,27 +69,32 @@ class DynamicPropertiesContextCustomizer implements ContextCustomizer {
|
||||
() -> "@DynamicPropertySource method '" + method.getName() + "' must be static");
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
Assert.state(types.length == 1 && types[0] == DynamicPropertyRegistry.class,
|
||||
() -> "@DynamicPropertySource method '" + method.getName() + "' must accept a single DynamicPropertyRegistry argument");
|
||||
() -> "@DynamicPropertySource method '" + method.getName() +
|
||||
"' must accept a single DynamicPropertyRegistry argument");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
MutablePropertySources sources = context.getEnvironment().getPropertySources();
|
||||
sources.addFirst(new DynamicValuesPropertySource(PROPERTY_SOURCE_NAME, buildDynamicPropertiesMap()));
|
||||
}
|
||||
DynamicValuesPropertySource propertySource = getOrAdd(context.getEnvironment());
|
||||
|
||||
if (!context.containsBean(DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME)) {
|
||||
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
||||
beanFactory.registerSingleton(DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME, propertySource.dynamicPropertyRegistry);
|
||||
}
|
||||
|
||||
if (!context.containsBean(DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME)) {
|
||||
if (!(context.getBeanFactory() instanceof BeanDefinitionRegistry registry)) {
|
||||
throw new IllegalStateException("BeanFactory must be a BeanDefinitionRegistry");
|
||||
}
|
||||
BeanDefinition beanDefinition = new RootBeanDefinition(DynamicPropertySourceBeanInitializer.class);
|
||||
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
registry.registerBeanDefinition(DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME, beanDefinition);
|
||||
}
|
||||
|
||||
private Map<String, Supplier<Object>> buildDynamicPropertiesMap() {
|
||||
Map<String, Supplier<Object>> map = new LinkedHashMap<>();
|
||||
DynamicPropertyRegistry dynamicPropertyRegistry = (name, valueSupplier) -> {
|
||||
Assert.hasText(name, "'name' must not be null or blank");
|
||||
Assert.notNull(valueSupplier, "'valueSupplier' must not be null");
|
||||
map.put(name, valueSupplier);
|
||||
};
|
||||
this.methods.forEach(method -> {
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
ReflectionUtils.invokeMethod(method, null, dynamicPropertyRegistry);
|
||||
ReflectionUtils.invokeMethod(method, null, propertySource.dynamicPropertyRegistry);
|
||||
});
|
||||
return Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
Set<Method> getMethods() {
|
||||
@@ -100,4 +113,17 @@ class DynamicPropertiesContextCustomizer implements ContextCustomizer {
|
||||
return this.methods.hashCode();
|
||||
}
|
||||
|
||||
|
||||
private static DynamicValuesPropertySource getOrAdd(ConfigurableEnvironment environment) {
|
||||
PropertySource<?> propertySource = environment.getPropertySources()
|
||||
.get(DynamicValuesPropertySource.PROPERTY_SOURCE_NAME);
|
||||
if (propertySource == null) {
|
||||
environment.getPropertySources().addFirst(new DynamicValuesPropertySource());
|
||||
return getOrAdd(environment);
|
||||
}
|
||||
Assert.state(propertySource instanceof DynamicValuesPropertySource,
|
||||
"Incorrect DynamicValuesPropertySource type registered");
|
||||
return (DynamicValuesPropertySource) propertySource;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -26,12 +27,15 @@ import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
|
||||
/**
|
||||
* {@link ContextCustomizerFactory} to support
|
||||
* {@link DynamicPropertySource @DynamicPropertySource} methods.
|
||||
* {@link ContextCustomizerFactory} which supports
|
||||
* {@link DynamicPropertySource @DynamicPropertySource} methods and the
|
||||
* registration of a {@link DynamicPropertyRegistry} as a singleton bean in the
|
||||
* container for use in {@code @Configuration} classes and {@code @Bean} methods.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
@@ -49,7 +53,7 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact
|
||||
Set<Method> methods = new LinkedHashSet<>();
|
||||
findMethods(testClass, methods);
|
||||
if (methods.isEmpty()) {
|
||||
return null;
|
||||
methods = Collections.emptySet();
|
||||
}
|
||||
return new DynamicPropertiesContextCustomizer(methods);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.context.weaving.LoadTimeWeaverAware;
|
||||
import org.springframework.instrument.classloading.LoadTimeWeaver;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
/**
|
||||
* Internal component which eagerly initializes beans created by {@code @Bean}
|
||||
* factory methods annotated with {@link DynamicPropertySource @DynamicPropertySource}.
|
||||
*
|
||||
* <p>This class implements {@link LoadTimeWeaverAware} since doing so is
|
||||
* currently the only way to have a component eagerly initialized before the
|
||||
* {@code ConfigurableListableBeanFactory.preInstantiateSingletons()} phase.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.2
|
||||
*/
|
||||
class DynamicPropertySourceBeanInitializer implements BeanFactoryAware, InitializingBean, LoadTimeWeaverAware {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DynamicPropertySourceBeanInitializer.class);
|
||||
|
||||
@Nullable
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
if (!(this.beanFactory instanceof ListableBeanFactory lbf)) {
|
||||
throw new IllegalStateException("BeanFactory must be set and must be a ListableBeanFactory");
|
||||
}
|
||||
for (String name : lbf.getBeanNamesForAnnotation(DynamicPropertySource.class)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Eagerly initializing @DynamicPropertySource bean '%s'".formatted(name));
|
||||
}
|
||||
this.beanFactory.getBean(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,11 +16,15 @@
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.function.SupplierUtils;
|
||||
|
||||
/**
|
||||
@@ -33,9 +37,22 @@ import org.springframework.util.function.SupplierUtils;
|
||||
*/
|
||||
class DynamicValuesPropertySource extends MapPropertySource {
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
DynamicValuesPropertySource(String name, Map<String, Supplier<Object>> valueSuppliers) {
|
||||
super(name, (Map) valueSuppliers);
|
||||
static final String PROPERTY_SOURCE_NAME = "Dynamic Test Properties";
|
||||
|
||||
final DynamicPropertyRegistry dynamicPropertyRegistry;
|
||||
|
||||
|
||||
DynamicValuesPropertySource() {
|
||||
this(Collections.synchronizedMap(new LinkedHashMap<>()));
|
||||
}
|
||||
|
||||
DynamicValuesPropertySource(Map<String, Supplier<Object>> valueSuppliers) {
|
||||
super(PROPERTY_SOURCE_NAME, Collections.unmodifiableMap(valueSuppliers));
|
||||
this.dynamicPropertyRegistry = (name, valueSupplier) -> {
|
||||
Assert.hasText(name, "'name' must not be null or blank");
|
||||
Assert.notNull(valueSupplier, "'valueSupplier' must not be null");
|
||||
valueSuppliers.put(name, valueSupplier);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user