Skip to content

Commit b3022fd

Browse files
author
Dave Syer
committed
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
1 parent 4348172 commit b3022fd

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3232
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3333
import org.springframework.boot.SpringApplication;
34-
import org.springframework.boot.bind.PropertySourcesPropertyValues;
35-
import org.springframework.boot.bind.RelaxedDataBinder;
34+
import org.springframework.boot.bind.PropertiesConfigurationFactory;
3635
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
3736
import org.springframework.boot.context.event.ApplicationPreparedEvent;
3837
import org.springframework.boot.env.EnumerableCompositePropertySource;
@@ -54,6 +53,7 @@
5453
import org.springframework.core.io.ResourceLoader;
5554
import org.springframework.util.Assert;
5655
import org.springframework.util.StringUtils;
56+
import org.springframework.validation.BindException;
5757

5858
/**
5959
* {@link ApplicationListener} that configures the context environment by loading
@@ -171,9 +171,17 @@ protected void addPropertySources(ConfigurableEnvironment environment,
171171
*/
172172
protected void bindToSpringApplication(ConfigurableEnvironment environment,
173173
SpringApplication application) {
174-
RelaxedDataBinder binder = new RelaxedDataBinder(application, "spring.main");
174+
PropertiesConfigurationFactory<SpringApplication> binder = new PropertiesConfigurationFactory<SpringApplication>(
175+
application);
176+
binder.setTargetName("spring.main");
175177
binder.setConversionService(this.conversionService);
176-
binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources()));
178+
binder.setPropertySources(environment.getPropertySources());
179+
try {
180+
binder.bindPropertiesToTarget();
181+
}
182+
catch (BindException e) {
183+
throw new IllegalStateException("Cannot bind to SpringApplication", e);
184+
}
177185
}
178186

179187
/**

spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class ConfigFileApplicationListenerTests {
8282
public void cleanup() {
8383
System.clearProperty("my.property");
8484
System.clearProperty("spring.config.location");
85+
System.clearProperty("spring.main.showBanner");
8586
}
8687

8788
@Test
@@ -561,6 +562,17 @@ public void bindsToSpringApplication() throws Exception {
561562
assertThat((Boolean) field.get(application), equalTo(false));
562563
}
563564

565+
@Test
566+
public void bindsSystemPropertyToSpringApplication() throws Exception {
567+
// gh-951
568+
System.setProperty("spring.main.showBanner", "false");
569+
this.initializer.onApplicationEvent(this.event);
570+
SpringApplication application = this.event.getSpringApplication();
571+
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
572+
field.setAccessible(true);
573+
assertThat((Boolean) field.get(application), equalTo(false));
574+
}
575+
564576
private static Matcher<? super ConfigurableEnvironment> containsPropertySource(
565577
final String sourceName) {
566578
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {

0 commit comments

Comments
 (0)