From 564475556e3e5567ce00825ce97bfff3ede79a8b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 26 Nov 2013 09:49:37 +0000 Subject: [PATCH] Allow for commandLineArgs to be already present in Environment --- .../boot/SpringApplication.java | 23 +++++++--- .../boot/SpringApplicationTests.java | 43 ++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 0203f039ca..63a4a4dd83 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -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)); + } } } diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 650254f143..e4b9e5f4e1 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -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 + . 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