From b3022fd24aef7500e01490f23e33e24f17dca780 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 26 May 2014 10:40:24 +0100 Subject: [PATCH] Use PropertiesConfigurationFactory to bind to SpringApplication Then non-enumerable property sources will be accessible (like SystemProperties in principle). This is the same way that other beans are bound to the environment, but this one never got the same treatment. Fixes gh-951, gh-934 --- .../config/ConfigFileApplicationListener.java | 16 ++++++++++++---- .../ConfigFileApplicationListenerTests.java | 12 ++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) 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 e0810bc9207..5ba71a07c3c 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 @@ -31,8 +31,7 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.SpringApplication; -import org.springframework.boot.bind.PropertySourcesPropertyValues; -import org.springframework.boot.bind.RelaxedDataBinder; +import org.springframework.boot.bind.PropertiesConfigurationFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.env.EnumerableCompositePropertySource; @@ -54,6 +53,7 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import org.springframework.validation.BindException; /** * {@link ApplicationListener} that configures the context environment by loading @@ -171,9 +171,17 @@ public class ConfigFileApplicationListener implements */ protected void bindToSpringApplication(ConfigurableEnvironment environment, SpringApplication application) { - RelaxedDataBinder binder = new RelaxedDataBinder(application, "spring.main"); + PropertiesConfigurationFactory binder = new PropertiesConfigurationFactory( + application); + binder.setTargetName("spring.main"); binder.setConversionService(this.conversionService); - binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources())); + binder.setPropertySources(environment.getPropertySources()); + try { + binder.bindPropertiesToTarget(); + } + catch (BindException e) { + throw new IllegalStateException("Cannot bind to SpringApplication", e); + } } /** 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 446cf6736c3..5c3a10075d4 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 @@ -82,6 +82,7 @@ public class ConfigFileApplicationListenerTests { public void cleanup() { System.clearProperty("my.property"); System.clearProperty("spring.config.location"); + System.clearProperty("spring.main.showBanner"); } @Test @@ -561,6 +562,17 @@ public class ConfigFileApplicationListenerTests { assertThat((Boolean) field.get(application), equalTo(false)); } + @Test + public void bindsSystemPropertyToSpringApplication() throws Exception { + // gh-951 + System.setProperty("spring.main.showBanner", "false"); + 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 containsPropertySource( final String sourceName) { return new TypeSafeDiagnosingMatcher() {