Extract file-based property sources during bootstrap
The bootstrap.properties need to stick with the default properties during the process of initializing the application.properties otherwise the ordering ends up wrong because of the addLast() semantics in an Environment merge. To do this is a bit ugly with the additional constraint that the default properties has to remain a MapPropertySource (so that other processors can append to it if needed). So we created a custom extension of MapPropertySource that also carries all the bootstrap properties during the phase where the application.properties are being processed, but unpacks them as soon as possible afterwards in an ApplicationContextInitializer, preserving the order, but making the bootstrap.properties available effectively for the whole of the startup and initializaion phase of the main context.
This commit is contained in:
@@ -28,6 +28,7 @@ import org.junit.rules.ExpectedException;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
@@ -81,6 +82,24 @@ public class BootstrapConfigurationTests {
|
||||
this.context.getEnvironment().getPropertySources().contains("bootstrap"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bootstrapPropertiesAvailableInInitializer() {
|
||||
this.context = new SpringApplicationBuilder().web(false)
|
||||
.sources(BareConfiguration.class).initializers(
|
||||
new ApplicationContextInitializer<ConfigurableApplicationContext>() {
|
||||
@Override
|
||||
public void initialize(
|
||||
ConfigurableApplicationContext applicationContext) {
|
||||
// This property is defined in bootstrap.properties
|
||||
assertEquals("child", applicationContext.getEnvironment()
|
||||
.getProperty("info.name"));
|
||||
}
|
||||
})
|
||||
.run();
|
||||
assertTrue(
|
||||
this.context.getEnvironment().getPropertySources().contains("bootstrap"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Running the test from maven will start from a different directory then starting it
|
||||
* from intellij
|
||||
@@ -105,7 +124,6 @@ public class BootstrapConfigurationTests {
|
||||
@Test
|
||||
public void picksUpAdditionalPropertySource() {
|
||||
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
|
||||
System.setProperty("expected.name", "bootstrap");
|
||||
this.context = new SpringApplicationBuilder().web(false)
|
||||
.sources(BareConfiguration.class).run();
|
||||
assertEquals("bar", this.context.getEnvironment().getProperty("bootstrap.foo"));
|
||||
|
||||
Reference in New Issue
Block a user