Browse Source

Make sure cli args are copied to bootstrap.

pull/274/head
Spencer Gibb 7 years ago
parent
commit
1bd2c3112a
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 6
      spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java
  2. 19
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java
  3. 23
      spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java
  4. 1
      spring-cloud-context/src/test/resources/bootstrap-refresh.properties

6
spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java

@ -16,6 +16,7 @@ import org.springframework.cloud.context.environment.EnvironmentChangeEvent; @@ -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 { @@ -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<String> standardSources = new HashSet<>(
Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,

19
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java

@ -1,8 +1,12 @@ @@ -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 @@ -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<String> 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<ConfigurableApplicationContext> customInitializer() {
return new ApplicationContextInitializer<ConfigurableApplicationContext>() {

23
spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ContextRefresherTests.java

@ -9,10 +9,10 @@ import java.util.Map; @@ -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 { @@ -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<String> names(MutablePropertySources propertySources) {
List<String> list = new ArrayList<>();
for (PropertySource<?> p : propertySources) {

1
spring-cloud-context/src/test/resources/bootstrap-refresh.properties

@ -1 +1,2 @@ @@ -1 +1,2 @@
info.name: refresh-child
test.bootstrap.foo: refresh
Loading…
Cancel
Save