Fix property detection in SpringApplicationBuilder
Update SpringApplicationBuilder so that properties of the form `abc=d:e:f` are correctly parsed. Prior to this commit the `:` delimiter would always be chosen over `=`, even if `=` occurred first. Fixes gh-6121
This commit is contained in:
parent
f27bdcb737
commit
452281ca8d
|
@ -394,20 +394,28 @@ public class SpringApplicationBuilder {
|
|||
return properties(getMapFromKeyValuePairs(defaultProperties));
|
||||
}
|
||||
|
||||
private Map<String, Object> getMapFromKeyValuePairs(String[] args) {
|
||||
private Map<String, Object> getMapFromKeyValuePairs(String[] properties) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
for (String pair : args) {
|
||||
int index = pair.indexOf(":");
|
||||
if (index <= 0) {
|
||||
index = pair.indexOf("=");
|
||||
}
|
||||
String key = pair.substring(0, index > 0 ? index : pair.length());
|
||||
String value = index > 0 ? pair.substring(index + 1) : "";
|
||||
for (String property : properties) {
|
||||
int index = lowestIndexOf(property, ":", "=");
|
||||
String key = property.substring(0, index > 0 ? index : property.length());
|
||||
String value = index > 0 ? property.substring(index + 1) : "";
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private int lowestIndexOf(String property, String... candidates) {
|
||||
int index = -1;
|
||||
for (String candidate : candidates) {
|
||||
int candidateIndex = property.indexOf(candidate);
|
||||
if (candidateIndex > 0) {
|
||||
index = (index == -1 ? candidateIndex : Math.min(index, candidateIndex));
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default properties for the environment in the form {@code key=value} or
|
||||
* {@code key:value}.
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
@ -91,6 +92,20 @@ public class SpringApplicationBuilderTests {
|
|||
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertiesWithRepeatSeparator() throws Exception {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder()
|
||||
.sources(ExampleConfig.class).contextClass(StaticApplicationContext.class)
|
||||
.properties("one=c:\\logging.file", "two=a:b", "three:c:\\logging.file",
|
||||
"four:a:b");
|
||||
this.context = application.run();
|
||||
ConfigurableEnvironment environment = this.context.getEnvironment();
|
||||
assertThat(environment.getProperty("one"), is(equalTo("c:\\logging.file")));
|
||||
assertThat(environment.getProperty("two"), is(equalTo("a:b")));
|
||||
assertThat(environment.getProperty("three"), is(equalTo("c:\\logging.file")));
|
||||
assertThat(environment.getProperty("four"), is(equalTo("a:b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void specificApplicationContextClass() throws Exception {
|
||||
SpringApplicationBuilder application = new SpringApplicationBuilder()
|
||||
|
|
Loading…
Reference in New Issue