SpringApplicationBuilder.properties fixes

Previously: properties(Map) behaved differently to
properties(String...).

Fixed by merging the implementations.

Also added properties(Properties).
pull/103/head
Dave Syer 11 years ago
parent 63a2d06767
commit e005ba7232

@ -17,10 +17,12 @@ package org.springframework.boot.builder;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
@ -326,13 +328,7 @@ public class SpringApplicationBuilder {
* @return the current builder
*/
public SpringApplicationBuilder properties(String... defaultProperties) {
this.defaultProperties.putAll(getMapFromKeyValuePairs(defaultProperties));
this.application.setDefaultProperties(this.defaultProperties);
if (this.parent != null) {
this.parent.properties(defaultProperties);
this.parent.environment(this.environment);
}
return this;
return properties(getMapFromKeyValuePairs(defaultProperties));
}
private Map<String, Object> getMapFromKeyValuePairs(String[] args) {
@ -349,6 +345,25 @@ public class SpringApplicationBuilder {
return map;
}
/**
* Default properties for the environment in the form <code>key=value</code> or
* <code>key:value</code>.
*
* @param defaultProperties the properties to set.
* @return the current builder
*/
public SpringApplicationBuilder properties(Properties defaultProperties) {
return properties(getMapFromProperties(defaultProperties));
}
private Map<String, Object> getMapFromProperties(Properties properties) {
HashMap<String, Object> map = new HashMap<String, Object>();
for (Object key : Collections.list(properties.propertyNames())) {
map.put((String) key, properties.get(key));
}
return map;
}
/**
* Default properties for the environment. Multiple calls to this method are
* cumulative.
@ -360,6 +375,11 @@ public class SpringApplicationBuilder {
*/
public SpringApplicationBuilder properties(Map<String, Object> defaults) {
this.defaultProperties.putAll(defaults);
this.application.setDefaultProperties(this.defaultProperties);
if (this.parent != null) {
this.parent.properties(this.defaultProperties);
this.parent.environment(this.environment);
}
return this;
}

@ -18,6 +18,7 @@ package org.springframework.boot.builder;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import org.junit.After;
import org.junit.Test;
@ -28,6 +29,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
@ -64,6 +66,28 @@ public class SpringApplicationBuilderTests {
assertThat(this.context.getEnvironment().acceptsProfiles("foo"), is(true));
}
@Test
public void propertiesAsMap() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder()
.sources(ExampleConfig.class)
.contextClass(StaticApplicationContext.class)
.properties(Collections.<String, Object> singletonMap("bar", "foo"));
this.context = application.run();
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo")));
}
@Test
public void propertiesAsProperties() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder()
.sources(ExampleConfig.class)
.contextClass(StaticApplicationContext.class)
.properties(
StringUtils.splitArrayElementsIntoProperties(
new String[] { "bar=foo" }, "="));
this.context = application.run();
assertThat(this.context.getEnvironment().getProperty("bar"), is(equalTo("foo")));
}
@Test
public void specificApplicationContextClass() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder().sources(

Loading…
Cancel
Save