forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce DynamicPropertyRegistrar support
This is a PROOF OF CONCEPT which introduces DynamicPropertyRegistrar support as a replacement for registering a DynamicPropertyRegistry as a bean in the ApplicationContext. It appears that most things work as expected; however, in this POC there are two instances of DefaultDynamicPropertyRegistry that prevent the use of @DynamicPropertySource and DynamicPropertyRegistrar in the same ApplicationContext, since the two features end up writing to two different valueSuppliers maps. The commented-out "magic.word" assertions in DynamicPropertySourceIntegrationTests therefore currently fail. See spring-projectsgh-33501
- Loading branch information
Showing
9 changed files
with
194 additions
and
88 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
spring-test/src/main/java/org/springframework/test/context/DynamicPropertyRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Registrar that is used to add properties with dynamically resolved values to | ||
* the {@code Environment} via a {@link DynamicPropertyRegistry}. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
* @see DynamicPropertySource | ||
* @see DynamicPropertyRegistry | ||
*/ | ||
@FunctionalInterface | ||
public interface DynamicPropertyRegistrar { | ||
|
||
void accept(DynamicPropertyRegistry registry); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ava/org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.BeanFactoryInitializer; | ||
import org.springframework.beans.factory.BeanFactoryUtils; | ||
import org.springframework.beans.factory.ListableBeanFactory; | ||
import org.springframework.context.EnvironmentAware; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.test.context.DynamicPropertyRegistrar; | ||
|
||
/** | ||
* Internal component which eagerly initializes {@link DynamicPropertyRegistrar} | ||
* beans. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
*/ | ||
class DynamicPropertyRegistrarBeanInitializer implements BeanFactoryInitializer<ListableBeanFactory>, EnvironmentAware { | ||
|
||
private static final Log logger = LogFactory.getLog(DynamicPropertyRegistrarBeanInitializer.class); | ||
|
||
|
||
@Nullable | ||
private ConfigurableEnvironment environment; | ||
|
||
|
||
@Override | ||
public void setEnvironment(Environment environment) { | ||
if (!(environment instanceof ConfigurableEnvironment configurableEnvironment)) { | ||
throw new IllegalArgumentException("Environment must be a ConfigurableEnvironment"); | ||
} | ||
this.environment = configurableEnvironment; | ||
} | ||
|
||
@Override | ||
public void initialize(ListableBeanFactory beanFactory) { | ||
if (this.environment == null) { | ||
throw new IllegalStateException("Environment is required"); | ||
} | ||
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( | ||
beanFactory, DynamicPropertyRegistrar.class); | ||
if (beanNames.length > 0) { | ||
DefaultDynamicPropertyRegistry dynamicPropertyRegistry = | ||
new DefaultDynamicPropertyRegistry(this.environment, true); | ||
|
||
for (String name : beanNames) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Eagerly initializing DynamicPropertyRegistrar bean '%s'".formatted(name)); | ||
} | ||
DynamicPropertyRegistrar registrar = beanFactory.getBean(name, DynamicPropertyRegistrar.class); | ||
registrar.accept(dynamicPropertyRegistry); | ||
} | ||
} | ||
} | ||
|
||
} |
48 changes: 0 additions & 48 deletions
48
...n/java/org/springframework/test/context/support/DynamicPropertySourceBeanInitializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.