diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java index 0b6dbd78..6dd1f129 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java @@ -16,6 +16,7 @@ import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; @@ -33,8 +34,9 @@ public class ContextRefresher { private static final String REFRESH_ARGS_PROPERTY_SOURCE = "refreshArgs"; - private static final String[] DEFAULT_PROPERTY_SOURCES = new String[] { - "defaultProperties" }; + private static final String[] DEFAULT_PROPERTY_SOURCES = new String[] { //order matters, cli args aren't first, things get messy + CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, + "defaultProperties"}; private Set standardSources = new HashSet<>( Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java index 7265a3a2..9b10593c 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java @@ -1,8 +1,12 @@ package org.springframework.cloud.bootstrap; import java.util.Collections; +import java.util.List; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -17,12 +21,27 @@ import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapCon */ @Order(0) @Configuration +@EnableConfigurationProperties public class TestBootstrapConfiguration { public TestBootstrapConfiguration() { firstToBeCreated.compareAndSet(null, TestBootstrapConfiguration.class); } + public static List fooSightings = null; + + @Bean + @Qualifier("foo-during-bootstrap") + public String fooDuringBootstrap(ConfigurableEnvironment environment, ApplicationEventPublisher publisher) { + String property = environment.getProperty("test.bootstrap.foo", "undefined"); + + if (fooSightings != null) { + fooSightings.add(property); + } + + return property; + } + @Bean public ApplicationContextInitializer customInitializer() { return new ApplicationContextInitializer() { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java index cba9d2f5..47417a82 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java @@ -9,10 +9,10 @@ import java.util.Map; import org.junit.After; import org.junit.Test; import org.mockito.Mockito; - import org.springframework.boot.SpringApplication; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.cloud.bootstrap.TestBootstrapConfiguration; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ConfigurableApplicationContext; @@ -113,6 +113,27 @@ public class ContextRefresherTests { } } + @Test + @SuppressWarnings("unchecked") + public void commandLineArgsPassedToBootstrapConfiguration() { + + TestBootstrapConfiguration.fooSightings = new ArrayList<>(); + + try (ConfigurableApplicationContext context = SpringApplication.run(ContextRefresherTests.class, + "--spring.main.webEnvironment=false", "--debug=false", + "--spring.main.bannerMode=OFF", + "--spring.cloud.bootstrap.name=refresh", + "--test.bootstrap.foo=bar")) { + context.getEnvironment().setActiveProfiles("refresh"); + ContextRefresher refresher = new ContextRefresher(context, scope); + refresher.refresh(); + assertThat(TestBootstrapConfiguration.fooSightings).containsExactly("bar", "bar"); + } + + TestBootstrapConfiguration.fooSightings = null; + } + + private List names(MutablePropertySources propertySources) { List list = new ArrayList<>(); for (PropertySource p : propertySources) { diff --git a/spring-cloud-context/src/test/resources/bootstrap-refresh.properties b/spring-cloud-context/src/test/resources/bootstrap-refresh.properties index 1339cc1d..7c55d8a5 100644 --- a/spring-cloud-context/src/test/resources/bootstrap-refresh.properties +++ b/spring-cloud-context/src/test/resources/bootstrap-refresh.properties @@ -1 +1,2 @@ info.name: refresh-child +test.bootstrap.foo: refresh \ No newline at end of file