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));
|
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>();
|
Map<String, Object> map = new HashMap<String, Object>();
|
||||||
for (String pair : args) {
|
for (String property : properties) {
|
||||||
int index = pair.indexOf(":");
|
int index = lowestIndexOf(property, ":", "=");
|
||||||
if (index <= 0) {
|
String key = property.substring(0, index > 0 ? index : property.length());
|
||||||
index = pair.indexOf("=");
|
String value = index > 0 ? property.substring(index + 1) : "";
|
||||||
}
|
|
||||||
String key = pair.substring(0, index > 0 ? index : pair.length());
|
|
||||||
String value = index > 0 ? pair.substring(index + 1) : "";
|
|
||||||
map.put(key, value);
|
map.put(key, value);
|
||||||
}
|
}
|
||||||
return map;
|
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
|
* Default properties for the environment in the form {@code key=value} or
|
||||||
* {@code key:value}.
|
* {@code key:value}.
|
||||||
|
|
|
@ -31,6 +31,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
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.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.StandardEnvironment;
|
import org.springframework.core.env.StandardEnvironment;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
@ -91,6 +92,20 @@ public class SpringApplicationBuilderTests {
|
||||||
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo")));
|
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
|
@Test
|
||||||
public void specificApplicationContextClass() throws Exception {
|
public void specificApplicationContextClass() throws Exception {
|
||||||
SpringApplicationBuilder application = new SpringApplicationBuilder()
|
SpringApplicationBuilder application = new SpringApplicationBuilder()
|
||||||
|
|
Loading…
Reference in New Issue