Allow for commandLineArgs to be already present in Environment
This commit is contained in:
@@ -51,9 +51,11 @@ import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.env.CommandLinePropertySource;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.env.SimpleCommandLinePropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
@@ -375,13 +377,24 @@ public class SpringApplication {
|
||||
* @param args run arguments
|
||||
*/
|
||||
protected void addPropertySources(ConfigurableEnvironment environment, String[] args) {
|
||||
MutablePropertySources sources = environment.getPropertySources();
|
||||
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
|
||||
environment.getPropertySources().addLast(
|
||||
new MapPropertySource("defaultProperties", this.defaultProperties));
|
||||
sources.addLast(new MapPropertySource("defaultProperties",
|
||||
this.defaultProperties));
|
||||
}
|
||||
if (this.addCommandLineProperties) {
|
||||
environment.getPropertySources().addFirst(
|
||||
new SimpleCommandLinePropertySource(args));
|
||||
if (this.addCommandLineProperties && args.length > 0) {
|
||||
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
|
||||
if (sources.contains(name)) {
|
||||
PropertySource<?> source = sources.get(name);
|
||||
CompositePropertySource composite = new CompositePropertySource(name);
|
||||
composite.addPropertySource(new SimpleCommandLinePropertySource(name
|
||||
+ "-" + args.hashCode(), args));
|
||||
composite.addPropertySource(source);
|
||||
sources.replace(name, composite);
|
||||
}
|
||||
else {
|
||||
sources.addFirst(new SimpleCommandLinePropertySource(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@@ -43,6 +44,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.CommandLinePropertySource;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
@@ -58,6 +60,7 @@ import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -244,14 +247,41 @@ public class SpringApplicationTests {
|
||||
|
||||
@Test
|
||||
public void commandLinePropertySource() throws Exception {
|
||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.setWebEnvironment(false);
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
application.setEnvironment(environment);
|
||||
application.run("--foo=bar");
|
||||
assertTrue(hasPropertySource(environment, CommandLinePropertySource.class,
|
||||
"commandLineArgs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commandLinePropertySourceEnhancesEnvironment() throws Exception {
|
||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.setWebEnvironment(false);
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
environment.getPropertySources().addFirst(
|
||||
new MapPropertySource("commandLineArgs", Collections
|
||||
.<String, Object> singletonMap("foo", "original")));
|
||||
application.setEnvironment(environment);
|
||||
application.run("--foo=bar", "--bar=foo");
|
||||
assertTrue(hasPropertySource(environment, CompositePropertySource.class,
|
||||
"commandLineArgs"));
|
||||
assertEquals("foo", environment.getProperty("bar"));
|
||||
// New command line properties take precedence
|
||||
assertEquals("bar", environment.getProperty("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyCommandLinePropertySourceNotAdded() throws Exception {
|
||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||
application.setWebEnvironment(false);
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
application.setEnvironment(environment);
|
||||
application.run();
|
||||
assertThat(
|
||||
hasPropertySource(environment, CommandLinePropertySource.class,
|
||||
"commandLineArgs"), equalTo(true));
|
||||
assertFalse(hasPropertySource(environment, PropertySource.class,
|
||||
"commandLineArgs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -261,10 +291,9 @@ public class SpringApplicationTests {
|
||||
application.setAddCommandLineProperties(false);
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
application.setEnvironment(environment);
|
||||
application.run();
|
||||
assertThat(
|
||||
hasPropertySource(environment, MapPropertySource.class, "commandLineArgs"),
|
||||
equalTo(false));
|
||||
application.run("--foo=bar");
|
||||
assertFalse(hasPropertySource(environment, PropertySource.class,
|
||||
"commandLineArgs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user