[bs-131] Use MapPropertySource instead of SimpleCommandLinePropertySource

[Fixes #50267831] --server.port=9000 not working on command line
This commit is contained in:
Dave Syer 2013-05-21 15:26:16 +01:00
parent caf1aab9db
commit f73fbfc901
2 changed files with 26 additions and 24 deletions

View File

@ -41,8 +41,8 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.CommandLinePropertySource; import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
@ -316,8 +316,10 @@ public class SpringApplication {
if (environment instanceof ConfigurableEnvironment) { if (environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment configurable = (ConfigurableEnvironment) environment; ConfigurableEnvironment configurable = (ConfigurableEnvironment) environment;
if (this.addCommandLineProperties) { if (this.addCommandLineProperties) {
PropertySource<?> propertySource = new SimpleCommandLinePropertySource( // Don't use SimpleCommandLinePropertySource (SPR-10579)
mergeCommandLineArgs(this.defaultCommandLineArgs, args)); PropertySource<?> propertySource = new MapPropertySource(
"commandLineArgs", mergeCommandLineArgs(
this.defaultCommandLineArgs, args));
configurable.getPropertySources().addFirst(propertySource); configurable.getPropertySources().addFirst(propertySource);
} }
} }
@ -330,38 +332,35 @@ public class SpringApplication {
* @param args the ones passed in at runtime * @param args the ones passed in at runtime
* @return a new command line * @return a new command line
*/ */
protected String[] mergeCommandLineArgs(String[] defaults, String[] args) { protected Map<String, Object> mergeCommandLineArgs(String[] defaults, String[] args) {
if (defaults == null || defaults.length == 0) { if (defaults == null) {
return args; defaults = new String[0];
} }
List<String> result = new ArrayList<String>(); List<String> nonopts = new ArrayList<String>();
Map<String, String> options = new LinkedHashMap<String, String>(); Map<String, Object> options = new LinkedHashMap<String, Object>();
for (String arg : defaults) { for (String arg : defaults) {
if (isOptionArg(arg)) { if (isOptionArg(arg)) {
addOptionArg(options, arg); addOptionArg(options, arg);
} else { } else {
result.add(arg); nonopts.add(arg);
} }
} }
for (String arg : args) { for (String arg : args) {
if (isOptionArg(arg)) { if (isOptionArg(arg)) {
addOptionArg(options, arg); addOptionArg(options, arg);
} else if (!result.contains(arg)) { } else if (!nonopts.contains(arg)) {
result.add(arg); nonopts.add(arg);
} }
} }
List<String> optionsList = new ArrayList<String>(); for (String key : nonopts) {
for (String key : options.keySet()) { options.put(key, "");
String value = options.get(key);
optionsList.add("--" + key + (value == null ? "" : "=" + value));
} }
result.addAll(0, optionsList);
return result.toArray(new String[result.size()]); return options;
} }
@ -369,10 +368,10 @@ public class SpringApplication {
return arg.startsWith("--"); return arg.startsWith("--");
} }
private void addOptionArg(Map<String, String> map, String arg) { private void addOptionArg(Map<String, Object> map, String arg) {
String optionText = arg.substring(2, arg.length()); String optionText = arg.substring(2, arg.length());
String optionName; String optionName;
String optionValue = null; String optionValue = "";
if (optionText.contains("=")) { if (optionText.contains("=")) {
optionName = optionText.substring(0, optionText.indexOf("=")); optionName = optionText.substring(0, optionText.indexOf("="));
optionValue = optionText.substring(optionText.indexOf("=") + 1, optionValue = optionText.substring(optionText.indexOf("=") + 1,

View File

@ -38,9 +38,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext; import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
@ -232,7 +232,8 @@ public class SpringApplicationTests {
ConfigurableEnvironment environment = new StandardEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment); application.setEnvironment(environment);
application.run(); application.run();
assertThat(hasPropertySource(environment, CommandLinePropertySource.class), assertThat(
hasPropertySource(environment, MapPropertySource.class, "commandLineArgs"),
equalTo(true)); equalTo(true));
} }
@ -244,7 +245,8 @@ public class SpringApplicationTests {
ConfigurableEnvironment environment = new StandardEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment); application.setEnvironment(environment);
application.run(); application.run();
assertThat(hasPropertySource(environment, CommandLinePropertySource.class), assertThat(
hasPropertySource(environment, MapPropertySource.class, "commandLineArgs"),
equalTo(false)); equalTo(false));
} }
@ -315,9 +317,10 @@ public class SpringApplicationTests {
} }
private boolean hasPropertySource(ConfigurableEnvironment environment, private boolean hasPropertySource(ConfigurableEnvironment environment,
Class<?> propertySourceClass) { Class<?> propertySourceClass, String name) {
for (PropertySource<?> source : environment.getPropertySources()) { for (PropertySource<?> source : environment.getPropertySources()) {
if (propertySourceClass.isInstance(source)) { if (propertySourceClass.isInstance(source)
&& (name == null || name.equals(source.getName()))) {
return true; return true;
} }
} }