-
Notifications
You must be signed in to change notification settings - Fork 40.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide ConfigFileApplicationListener replacement
Deprecate `ConfigFileApplicationListener` and provide a replacement mechanism that supports arbitrary config data imports. This commit updates the following areas: - Extract `EnvironmentPostProcessor` invocation logic from the `ConfigFileApplicationListener` to new dedicated listener. Also providing support for `Log` injection. - Extract `RandomPropertySource` adding logic from the `ConfigFileApplicationListener` to a dedicated class. - Migrate to the recently introduced `DefaultPropertiesPropertySource` class when moving the defaultProperties `PropertySource` - Replace processing logic with a phased approach to ensure that profile enablement happens in a distinct phase and that profiles can no longer be activated on an ad-hoc basis. - Provide a more predictable and logical import order for processing `application.properties` and `application.yml` files. - Add support for a `spring.config.import` property which can be used to import additional config data. Also provide a pluggable API allowing third-parties to resolve and load locations themselves. - Add `spring.config.activate.on-profile` support which replaces the existing `spring.profiles` property. - Add `spring.config.activate.on-cloud-platform` support which allows a config data document to be active only on a given cloud platform. - Support a `spring.config.use-legacy-processing` property allowing the previous processing logic to be used. Closes gh-22497 Co-authored-by: Madhura Bhave <mbhave@vmware.com>
- Loading branch information
Showing
96 changed files
with
8,130 additions
and
168 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
...n/java/org/springframework/boot/test/context/ConfigDataApplicationContextInitializer.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,63 @@ | ||
/* | ||
* Copyright 2012-2020 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.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; | ||
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; | ||
|
||
/** | ||
* {@link ApplicationContextInitializer} that can be used with the | ||
* {@link ContextConfiguration#initializers()} to trigger loading of {@link ConfigData} | ||
* such as {@literal application.properties}. | ||
* | ||
* @author Phillip Webb | ||
* @since 2.4.0 | ||
* @see ConfigDataEnvironmentPostProcessor | ||
*/ | ||
public class ConfigDataApplicationContextInitializer | ||
implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
ConfigurableEnvironment environment = applicationContext.getEnvironment(); | ||
RandomValuePropertySource.addToEnvironment(environment); | ||
new ConfigDataProcessor().addPropertySources(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); | ||
} | ||
|
||
} | ||
|
||
} |
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
55 changes: 55 additions & 0 deletions
55
...a/org/springframework/boot/test/context/ConfigDataApplicationContextInitializerTests.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,55 @@ | ||
/* | ||
* Copyright 2012-2020 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.boot.test.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ConfigDataApplicationContextInitializer}. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@DirtiesContext | ||
@ContextConfiguration(classes = ConfigDataApplicationContextInitializerTests.Config.class, | ||
initializers = ConfigDataApplicationContextInitializer.class) | ||
class ConfigDataApplicationContextInitializerTests { | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Test | ||
void initializerPopulatesEnvironment() { | ||
assertThat(this.environment.getProperty("foo")).isEqualTo("bucket"); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...ework/boot/test/context/ConfigDataApplicationContextInitializerWithLegacySwitchTests.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,57 @@ | ||
/* | ||
* Copyright 2012-2020 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.boot.test.context; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ConfigDataApplicationContextInitializer}. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@DirtiesContext | ||
@TestPropertySource(properties = "spring.config.use-legacy-processing=true") | ||
@ContextConfiguration(classes = ConfigDataApplicationContextInitializerWithLegacySwitchTests.Config.class, | ||
initializers = ConfigDataApplicationContextInitializer.class) | ||
class ConfigDataApplicationContextInitializerWithLegacySwitchTests { | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@Test | ||
void initializerPopulatesEnvironment() { | ||
assertThat(this.environment.getProperty("foo")).isEqualTo("bucket"); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
} | ||
|
||
} |
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.