mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
[bs-131] Use MapPropertySource instead of SimpleCommandLinePropertySource
[Fixes #50267831] --server.port=9000 not working on command line
This commit is contained in:
parent
caf1aab9db
commit
f73fbfc901
@ -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<String, Object> mergeCommandLineArgs(String[] defaults, String[] args) {
|
||||
|
||||
if (defaults == null || defaults.length == 0) {
|
||||
return args;
|
||||
if (defaults == null) {
|
||||
defaults = new String[0];
|
||||
}
|
||||
|
||||
List<String> result = new ArrayList<String>();
|
||||
Map<String, String> options = new LinkedHashMap<String, String>();
|
||||
List<String> nonopts = new ArrayList<String>();
|
||||
Map<String, Object> options = new LinkedHashMap<String, Object>();
|
||||
|
||||
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<String> optionsList = new ArrayList<String>();
|
||||
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<String, String> map, String arg) {
|
||||
private void addOptionArg(Map<String, Object> 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,
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user