Fix properties binding to SpringApplication
Fix ConfigFileApplicationListener to correctly bind `application.properties` to SpringApplication. Binding in RC2 failed due to the fact that `ConfigurationPropertySources` did not extend `EnumerablePropertySource`. Fixes gh-346
This commit is contained in:
parent
eaa05c6b6d
commit
97c258a2b4
|
|
@ -44,6 +44,7 @@ import org.springframework.core.Ordered;
|
|||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
|
@ -386,15 +387,26 @@ public class ConfigFileApplicationListener implements
|
|||
* Holds the configuration {@link PropertySource}s as they are loaded can relocate
|
||||
* them once configuration classes have been processed.
|
||||
*/
|
||||
static class ConfigurationPropertySources extends PropertySource<Object> {
|
||||
static class ConfigurationPropertySources extends
|
||||
EnumerablePropertySource<Collection<PropertySource<?>>> {
|
||||
|
||||
private static final String NAME = "applicationConfigurationProperties";
|
||||
|
||||
private final Collection<PropertySource<?>> sources;
|
||||
|
||||
private final String[] names;
|
||||
|
||||
public ConfigurationPropertySources(Collection<PropertySource<?>> sources) {
|
||||
super(NAME);
|
||||
super(NAME, sources);
|
||||
this.sources = sources;
|
||||
List<String> names = new ArrayList<String>();
|
||||
for (PropertySource<?> source : sources) {
|
||||
if (source instanceof EnumerablePropertySource) {
|
||||
names.addAll(Arrays.asList(((EnumerablePropertySource<?>) source)
|
||||
.getPropertyNames()));
|
||||
}
|
||||
}
|
||||
this.names = names.toArray(new String[names.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -418,6 +430,11 @@ public class ConfigFileApplicationListener implements
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getPropertyNames() {
|
||||
return this.names;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.context.config;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
|
|
@ -26,7 +27,6 @@ import org.junit.Rule;
|
|||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.config.ConfigFileApplicationListener;
|
||||
import org.springframework.boot.context.config.ConfigFileApplicationListener.ConfigurationPropertySources;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
|
|
@ -38,6 +38,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
|||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.SimpleCommandLinePropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
|
@ -352,6 +353,17 @@ public class ConfigFileApplicationListenerTests {
|
|||
assertThat(property, equalTo("baz"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindsToSpringApplication() throws Exception {
|
||||
// gh-346
|
||||
this.initializer.setSearchNames("bindtoapplication");
|
||||
this.initializer.onApplicationEvent(this.event);
|
||||
SpringApplication application = this.event.getSpringApplication();
|
||||
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
|
||||
field.setAccessible(true);
|
||||
assertThat((Boolean) field.get(application), equalTo(false));
|
||||
}
|
||||
|
||||
private static Matcher<? super ConfigurableEnvironment> containsProperySource(
|
||||
final String sourceName) {
|
||||
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
spring.main.show_banner=false
|
||||
Loading…
Reference in New Issue