From 97c258a2b41f361a1cb8a6790471f682d301e9fe Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 13 Feb 2014 10:08:59 -0800 Subject: [PATCH] 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 --- .../config/ConfigFileApplicationListener.java | 21 +++++++++++++++++-- .../ConfigFileApplicationListenerTests.java | 14 ++++++++++++- .../resources/bindtoapplication.properties | 1 + 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 spring-boot/src/test/resources/bindtoapplication.properties diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 8261b29546..5aed2fb021 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -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 { + static class ConfigurationPropertySources extends + EnumerablePropertySource>> { private static final String NAME = "applicationConfigurationProperties"; private final Collection> sources; + private final String[] names; + public ConfigurationPropertySources(Collection> sources) { - super(NAME); + super(NAME, sources); this.sources = sources; + List names = new ArrayList(); + 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; + } + } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 5d7f904c1c..782def8742 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -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 containsProperySource( final String sourceName) { return new TypeSafeDiagnosingMatcher() { diff --git a/spring-boot/src/test/resources/bindtoapplication.properties b/spring-boot/src/test/resources/bindtoapplication.properties new file mode 100644 index 0000000000..322672defd --- /dev/null +++ b/spring-boot/src/test/resources/bindtoapplication.properties @@ -0,0 +1 @@ +spring.main.show_banner=false