diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ConfigDataApplicationContextInitializer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ConfigDataApplicationContextInitializer.java index 40261ffe96..6509148457 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ConfigDataApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ConfigDataApplicationContextInitializer.java @@ -16,8 +16,6 @@ package org.springframework.boot.test.context; -import java.util.function.Supplier; - import org.springframework.boot.context.config.ConfigData; import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; import org.springframework.boot.env.DefaultPropertiesPropertySource; @@ -25,7 +23,6 @@ import org.springframework.boot.env.RandomValuePropertySource; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.io.ResourceLoader; import org.springframework.test.context.ContextConfiguration; /** @@ -44,20 +41,8 @@ public class ConfigDataApplicationContextInitializer public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); RandomValuePropertySource.addToEnvironment(environment); - new ConfigDataProcessor().addPropertySources(environment, applicationContext); + ConfigDataEnvironmentPostProcessor.applyTo(environment, applicationContext); DefaultPropertiesPropertySource.moveToEnd(environment); } - private static class ConfigDataProcessor extends ConfigDataEnvironmentPostProcessor { - - ConfigDataProcessor() { - super(Supplier::get); - } - - void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { - addPropertySources(environment, resourceLoader, null); - } - - } - } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessor.java index 53e45c9aae..aea72f181f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessor.java @@ -16,7 +16,10 @@ package org.springframework.boot.context.config; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.function.Supplier; import org.apache.commons.logging.Log; @@ -61,10 +64,10 @@ public class ConfigDataEnvironmentPostProcessor implements EnvironmentPostProces @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { - addPropertySources(environment, application.getResourceLoader(), application.getAdditionalProfiles()); + postProcessEnvironment(environment, application.getResourceLoader(), application.getAdditionalProfiles()); } - protected final void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader, + void postProcessEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, Collection additionalProfiles) { try { this.logger.trace("Post-processing environment to add config data"); @@ -93,6 +96,44 @@ public class ConfigDataEnvironmentPostProcessor implements EnvironmentPostProces return new LegacyConfigFileApplicationListener(this.logFactory.getLog(ConfigFileApplicationListener.class)); } + /** + * Apply {@link ConfigData} post-processing to an existing {@link Environment}. This + * method can be useful when working with an {@link Environment} that has been created + * directly and not necessarily as part of a {@link SpringApplication}. + * @param environment the environment to apply {@link ConfigData} to + */ + public static void applyTo(ConfigurableEnvironment environment) { + applyTo(environment, null, Collections.emptyList()); + } + + /** + * Apply {@link ConfigData} post-processing to an existing {@link Environment}. This + * method can be useful when working with an {@link Environment} that has been created + * directly and not necessarily as part of a {@link SpringApplication}. + * @param environment the environment to apply {@link ConfigData} to + * @param resourceLoader the resource loader to use + * @param additionalProfiles any additional profiles that should be applied + */ + public static void applyTo(ConfigurableEnvironment environment, ResourceLoader resourceLoader, + String... additionalProfiles) { + applyTo(environment, resourceLoader, Arrays.asList(additionalProfiles)); + } + + /** + * Apply {@link ConfigData} post-processing to an existing {@link Environment}. This + * method can be useful when working with an {@link Environment} that has been created + * directly and not necessarily as part of a {@link SpringApplication}. + * @param environment the environment to apply {@link ConfigData} to + * @param resourceLoader the resource loader to use + * @param additionalProfiles any additional profiles that should be applied + */ + public static void applyTo(ConfigurableEnvironment environment, ResourceLoader resourceLoader, + Collection additionalProfiles) { + new ConfigDataEnvironmentPostProcessor(Supplier::get).postProcessEnvironment(environment, resourceLoader, + additionalProfiles); + + } + @SuppressWarnings("deprecation") static class LegacyConfigFileApplicationListener extends ConfigFileApplicationListener { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java index b341ee9bf0..7cf97d7de7 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorTests.java @@ -118,4 +118,12 @@ class ConfigDataEnvironmentPostProcessorTests { verify(legacyListener).addPropertySources(eq(this.environment), any(DefaultResourceLoader.class)); } + @Test + void applyToAppliesPostProcessing() { + int before = this.environment.getPropertySources().size(); + ConfigDataEnvironmentPostProcessor.applyTo(this.environment, null, "dev"); + assertThat(this.environment.getPropertySources().size()).isGreaterThan(before); + assertThat(this.environment.getActiveProfiles()).containsExactly("dev"); + } + }