Browse Source
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
pull/32991/head
15 changed files with 507 additions and 89 deletions
@ -0,0 +1,73 @@
@@ -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
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,165 @@
@@ -0,0 +1,165 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.core.env.ConfigurableEnvironment; |
||||
import org.springframework.core.env.Environment; |
||||
import org.springframework.core.env.MutablePropertySources; |
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Integration tests for {@link DynamicPropertyRegistry} bean support. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 6.2 |
||||
* @see DynamicPropertySourceIntegrationTests |
||||
*/ |
||||
@SpringJUnitConfig |
||||
@TestPropertySource(properties = "api.url: https://example.com/test") |
||||
class DynamicPropertyRegistryIntegrationTests { |
||||
|
||||
private static final String API_URL = "api.url"; |
||||
|
||||
|
||||
@Test |
||||
void dynamicPropertySourceOverridesTestPropertySource(@Autowired ConfigurableEnvironment env) { |
||||
assertApiUrlIsDynamic(env.getProperty(API_URL)); |
||||
|
||||
MutablePropertySources propertySources = env.getPropertySources(); |
||||
assertThat(propertySources.size()).isGreaterThanOrEqualTo(4); |
||||
assertThat(propertySources.contains("Inlined Test Properties")).isTrue(); |
||||
assertThat(propertySources.contains("Dynamic Test Properties")).isTrue(); |
||||
assertThat(propertySources.get("Inlined Test Properties").getProperty(API_URL)).isEqualTo("https://example.com/test"); |
||||
assertThat(propertySources.get("Dynamic Test Properties").getProperty(API_URL)).isEqualTo("https://example.com/dynamic"); |
||||
} |
||||
|
||||
@Test |
||||
void testReceivesDynamicProperty(@Value("${api.url}") String apiUrl) { |
||||
assertApiUrlIsDynamic(apiUrl); |
||||
} |
||||
|
||||
@Test |
||||
void environmentInjectedServiceCanRetrieveDynamicProperty(@Autowired EnvironmentInjectedService service) { |
||||
assertApiUrlIsDynamic(service); |
||||
} |
||||
|
||||
@Test |
||||
void constructorInjectedServiceReceivesDynamicProperty(@Autowired ConstructorInjectedService service) { |
||||
assertApiUrlIsDynamic(service); |
||||
} |
||||
|
||||
@Test |
||||
void setterInjectedServiceReceivesDynamicProperty(@Autowired SetterInjectedService service) { |
||||
assertApiUrlIsDynamic(service); |
||||
} |
||||
|
||||
|
||||
private static void assertApiUrlIsDynamic(ApiUrlClient service) { |
||||
assertApiUrlIsDynamic(service.getApiUrl()); |
||||
} |
||||
|
||||
private static void assertApiUrlIsDynamic(String apiUrl) { |
||||
assertThat(apiUrl).isEqualTo("https://example.com/dynamic"); |
||||
} |
||||
|
||||
|
||||
@Configuration |
||||
@Import({ EnvironmentInjectedService.class, ConstructorInjectedService.class, SetterInjectedService.class }) |
||||
static class Config { |
||||
|
||||
// Annotating this @Bean method with @DynamicPropertySource ensures that
|
||||
// this bean will be instantiated before any other singleton beans in the
|
||||
// context which further ensures that the dynamic "api.url" property is
|
||||
// available to all standard singleton beans.
|
||||
@Bean |
||||
@DynamicPropertySource |
||||
ApiServer apiServer(DynamicPropertyRegistry registry) { |
||||
ApiServer apiServer = new ApiServer(); |
||||
registry.add(API_URL, apiServer::getUrl); |
||||
return apiServer; |
||||
} |
||||
|
||||
} |
||||
|
||||
interface ApiUrlClient { |
||||
|
||||
String getApiUrl(); |
||||
} |
||||
|
||||
static class EnvironmentInjectedService implements ApiUrlClient { |
||||
|
||||
private final Environment env; |
||||
|
||||
|
||||
EnvironmentInjectedService(Environment env) { |
||||
this.env = env; |
||||
} |
||||
|
||||
@Override |
||||
public String getApiUrl() { |
||||
return this.env.getProperty(API_URL); |
||||
} |
||||
} |
||||
|
||||
static class ConstructorInjectedService implements ApiUrlClient { |
||||
|
||||
private final String apiUrl; |
||||
|
||||
|
||||
ConstructorInjectedService(@Value("${api.url}") String apiUrl) { |
||||
this.apiUrl = apiUrl; |
||||
} |
||||
|
||||
@Override |
||||
public String getApiUrl() { |
||||
return this.apiUrl; |
||||
} |
||||
} |
||||
|
||||
static class SetterInjectedService implements ApiUrlClient { |
||||
|
||||
private String apiUrl; |
||||
|
||||
|
||||
@Autowired |
||||
void setApiUrl(@Value("${api.url}") String apiUrl) { |
||||
this.apiUrl = apiUrl; |
||||
} |
||||
|
||||
@Override |
||||
public String getApiUrl() { |
||||
return this.apiUrl; |
||||
} |
||||
} |
||||
|
||||
static class ApiServer { |
||||
|
||||
String getUrl() { |
||||
return "https://example.com/dynamic"; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue