Commit 35673b74 authored by Phillip Webb's avatar Phillip Webb

Add Binder to BootstrapContext

Update `ConfigDataEnvironment` so that it adds the initial `Binder`
to the `BootstrapContext` for `Bootstrappers` to use.

Closes gh-23401
parent 8b8d5ccb
...@@ -25,6 +25,7 @@ import java.util.Set; ...@@ -25,6 +25,7 @@ import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.DefaultPropertiesPropertySource; import org.springframework.boot.DefaultPropertiesPropertySource;
import org.springframework.boot.context.config.ConfigDataEnvironmentContributors.BinderOption; import org.springframework.boot.context.config.ConfigDataEnvironmentContributors.BinderOption;
...@@ -198,8 +199,12 @@ class ConfigDataEnvironment { ...@@ -198,8 +199,12 @@ class ConfigDataEnvironment {
*/ */
void processAndApply() { void processAndApply() {
ConfigDataImporter importer = new ConfigDataImporter(this.resolvers, this.loaders); ConfigDataImporter importer = new ConfigDataImporter(this.resolvers, this.loaders);
this.bootstrapContext.register(Binder.class, InstanceSupplier
.from(() -> this.contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE)));
ConfigDataEnvironmentContributors contributors = processInitial(this.contributors, importer); ConfigDataEnvironmentContributors contributors = processInitial(this.contributors, importer);
ConfigDataActivationContext activationContext = createActivationContext(contributors); Binder initialBinder = contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
this.bootstrapContext.register(Binder.class, InstanceSupplier.of(initialBinder));
ConfigDataActivationContext activationContext = createActivationContext(initialBinder);
contributors = processWithoutProfiles(contributors, importer, activationContext); contributors = processWithoutProfiles(contributors, importer, activationContext);
activationContext = withProfiles(contributors, activationContext); activationContext = withProfiles(contributors, activationContext);
contributors = processWithProfiles(contributors, importer, activationContext); contributors = processWithProfiles(contributors, importer, activationContext);
...@@ -212,11 +217,10 @@ class ConfigDataEnvironment { ...@@ -212,11 +217,10 @@ class ConfigDataEnvironment {
return contributors.withProcessedImports(importer, null); return contributors.withProcessedImports(importer, null);
} }
private ConfigDataActivationContext createActivationContext(ConfigDataEnvironmentContributors contributors) { private ConfigDataActivationContext createActivationContext(Binder initialBinder) {
this.logger.trace("Creating config data activation context from initial contributions"); this.logger.trace("Creating config data activation context from initial contributions");
Binder binder = contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
try { try {
return new ConfigDataActivationContext(this.environment, binder); return new ConfigDataActivationContext(this.environment, initialBinder);
} }
catch (BindException ex) { catch (BindException ex) {
if (ex.getCause() instanceof InactiveConfigDataAccessException) { if (ex.getCause() instanceof InactiveConfigDataAccessException) {
......
...@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
class ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests { class ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests {
private SpringApplication application; private SpringApplication application;
...@@ -50,6 +50,7 @@ class ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests { ...@@ -50,6 +50,7 @@ class ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests {
.run("--spring.config.import=classpath:application-bootstrap-registry-integration-tests.properties")) { .run("--spring.config.import=classpath:application-bootstrap-registry-integration-tests.properties")) {
LoaderHelper bean = context.getBean(TestConfigDataBootstrap.LoaderHelper.class); LoaderHelper bean = context.getBean(TestConfigDataBootstrap.LoaderHelper.class);
assertThat(bean).isNotNull(); assertThat(bean).isNotNull();
assertThat(bean.getBound()).isEqualTo("igotbound");
assertThat(bean.getLocation().getResolverHelper().getLocation()).isEqualTo("testbootstrap:test"); assertThat(bean.getLocation().getResolverHelper().getLocation()).isEqualTo("testbootstrap:test");
} }
} }
......
...@@ -19,15 +19,17 @@ package org.springframework.boot.context.config; ...@@ -19,15 +19,17 @@ package org.springframework.boot.context.config;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import org.springframework.boot.BootstrapContextClosedEvent; import org.springframework.boot.BootstrapContextClosedEvent;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier; import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
/** /**
* Test classes used with * Test classes used with
* {@link ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests} to show how * {@link ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests} to show how
* a bootstrap registry can be used. This example will create helper instances during * a bootstrap registry can be used. This example will create helper instances during
* result and load. It also shows how the helper can ultimately be registered as a bean. * result and load. It also shows how the helper can ultimately be registered as a bean.
* *
...@@ -57,7 +59,7 @@ class TestConfigDataBootstrap { ...@@ -57,7 +59,7 @@ class TestConfigDataBootstrap {
@Override @Override
public ConfigData load(ConfigDataLoaderContext context, Location location) throws IOException { public ConfigData load(ConfigDataLoaderContext context, Location location) throws IOException {
context.getBootstrapContext().registerIfAbsent(LoaderHelper.class, context.getBootstrapContext().registerIfAbsent(LoaderHelper.class,
InstanceSupplier.from(() -> new LoaderHelper(location))); (bootstrapContext) -> new LoaderHelper(location, () -> bootstrapContext.get(Binder.class)));
LoaderHelper helper = context.getBootstrapContext().get(LoaderHelper.class); LoaderHelper helper = context.getBootstrapContext().get(LoaderHelper.class);
context.getBootstrapContext().addCloseListener(helper); context.getBootstrapContext().addCloseListener(helper);
return new ConfigData( return new ConfigData(
...@@ -103,14 +105,21 @@ class TestConfigDataBootstrap { ...@@ -103,14 +105,21 @@ class TestConfigDataBootstrap {
private final Location location; private final Location location;
LoaderHelper(Location location) { private final Supplier<Binder> binder;
LoaderHelper(Location location, Supplier<Binder> binder) {
this.location = location; this.location = location;
this.binder = binder;
} }
Location getLocation() { Location getLocation() {
return this.location; return this.location;
} }
String getBound() {
return this.binder.get().bind("myprop", String.class).orElse(null);
}
@Override @Override
public void onApplicationEvent(BootstrapContextClosedEvent event) { public void onApplicationEvent(BootstrapContextClosedEvent event) {
event.getApplicationContext().getBeanFactory().registerSingleton("loaderHelper", this); event.getApplicationContext().getBeanFactory().registerSingleton("loaderHelper", this);
......
spring.config.import=testbootstrap:test spring.config.import=testbootstrap:test
\ No newline at end of file myprop=igotbound
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment