diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/SpringApplication.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/SpringApplication.java index a95ec4a9f95..fba9646b7b8 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/SpringApplication.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/SpringApplication.java @@ -41,8 +41,8 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; -import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.SpringFactoriesLoader; @@ -316,8 +316,10 @@ public class SpringApplication { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment configurable = (ConfigurableEnvironment) environment; if (this.addCommandLineProperties) { - PropertySource propertySource = new SimpleCommandLinePropertySource( - mergeCommandLineArgs(this.defaultCommandLineArgs, args)); + // Don't use SimpleCommandLinePropertySource (SPR-10579) + PropertySource propertySource = new MapPropertySource( + "commandLineArgs", mergeCommandLineArgs( + this.defaultCommandLineArgs, args)); configurable.getPropertySources().addFirst(propertySource); } } @@ -330,38 +332,35 @@ public class SpringApplication { * @param args the ones passed in at runtime * @return a new command line */ - protected String[] mergeCommandLineArgs(String[] defaults, String[] args) { + protected Map mergeCommandLineArgs(String[] defaults, String[] args) { - if (defaults == null || defaults.length == 0) { - return args; + if (defaults == null) { + defaults = new String[0]; } - List result = new ArrayList(); - Map options = new LinkedHashMap(); + List nonopts = new ArrayList(); + Map options = new LinkedHashMap(); for (String arg : defaults) { if (isOptionArg(arg)) { addOptionArg(options, arg); } else { - result.add(arg); + nonopts.add(arg); } } for (String arg : args) { if (isOptionArg(arg)) { addOptionArg(options, arg); - } else if (!result.contains(arg)) { - result.add(arg); + } else if (!nonopts.contains(arg)) { + nonopts.add(arg); } } - List optionsList = new ArrayList(); - for (String key : options.keySet()) { - String value = options.get(key); - optionsList.add("--" + key + (value == null ? "" : "=" + value)); + for (String key : nonopts) { + options.put(key, ""); } - result.addAll(0, optionsList); - return result.toArray(new String[result.size()]); + return options; } @@ -369,10 +368,10 @@ public class SpringApplication { return arg.startsWith("--"); } - private void addOptionArg(Map map, String arg) { + private void addOptionArg(Map map, String arg) { String optionText = arg.substring(2, arg.length()); String optionName; - String optionValue = null; + String optionValue = ""; if (optionText.contains("=")) { optionName = optionText.substring(0, optionText.indexOf("=")); optionValue = optionText.substring(optionText.indexOf("=") + 1, diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java index 63d4853b20f..7b2ab60c00a 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java @@ -38,9 +38,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.Ordered; -import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; @@ -232,7 +232,8 @@ public class SpringApplicationTests { ConfigurableEnvironment environment = new StandardEnvironment(); application.setEnvironment(environment); application.run(); - assertThat(hasPropertySource(environment, CommandLinePropertySource.class), + assertThat( + hasPropertySource(environment, MapPropertySource.class, "commandLineArgs"), equalTo(true)); } @@ -244,7 +245,8 @@ public class SpringApplicationTests { ConfigurableEnvironment environment = new StandardEnvironment(); application.setEnvironment(environment); application.run(); - assertThat(hasPropertySource(environment, CommandLinePropertySource.class), + assertThat( + hasPropertySource(environment, MapPropertySource.class, "commandLineArgs"), equalTo(false)); } @@ -315,9 +317,10 @@ public class SpringApplicationTests { } private boolean hasPropertySource(ConfigurableEnvironment environment, - Class propertySourceClass) { + Class propertySourceClass, String name) { for (PropertySource source : environment.getPropertySources()) { - if (propertySourceClass.isInstance(source)) { + if (propertySourceClass.isInstance(source) + && (name == null || name.equals(source.getName()))) { return true; } }