Skip to content

Commit 35673b7

Browse files
committed
Add Binder to BootstrapContext
Update `ConfigDataEnvironment` so that it adds the initial `Binder` to the `BootstrapContext` for `Bootstrappers` to use. Closes gh-23401
1 parent 8b8d5cc commit 35673b7

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.commons.logging.Log;
2727

28+
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
2829
import org.springframework.boot.ConfigurableBootstrapContext;
2930
import org.springframework.boot.DefaultPropertiesPropertySource;
3031
import org.springframework.boot.context.config.ConfigDataEnvironmentContributors.BinderOption;
@@ -198,8 +199,12 @@ private ConfigDataEnvironmentContributor createInitialImportContributor(String l
198199
*/
199200
void processAndApply() {
200201
ConfigDataImporter importer = new ConfigDataImporter(this.resolvers, this.loaders);
202+
this.bootstrapContext.register(Binder.class, InstanceSupplier
203+
.from(() -> this.contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE)));
201204
ConfigDataEnvironmentContributors contributors = processInitial(this.contributors, importer);
202-
ConfigDataActivationContext activationContext = createActivationContext(contributors);
205+
Binder initialBinder = contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
206+
this.bootstrapContext.register(Binder.class, InstanceSupplier.of(initialBinder));
207+
ConfigDataActivationContext activationContext = createActivationContext(initialBinder);
203208
contributors = processWithoutProfiles(contributors, importer, activationContext);
204209
activationContext = withProfiles(contributors, activationContext);
205210
contributors = processWithProfiles(contributors, importer, activationContext);
@@ -212,11 +217,10 @@ private ConfigDataEnvironmentContributors processInitial(ConfigDataEnvironmentCo
212217
return contributors.withProcessedImports(importer, null);
213218
}
214219

215-
private ConfigDataActivationContext createActivationContext(ConfigDataEnvironmentContributors contributors) {
220+
private ConfigDataActivationContext createActivationContext(Binder initialBinder) {
216221
this.logger.trace("Creating config data activation context from initial contributions");
217-
Binder binder = contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
218222
try {
219-
return new ConfigDataActivationContext(this.environment, binder);
223+
return new ConfigDataActivationContext(this.environment, initialBinder);
220224
}
221225
catch (BindException ex) {
222226
if (ex.getCause() instanceof InactiveConfigDataAccessException) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Phillip Webb
3636
*/
37-
class ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests {
37+
class ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests {
3838

3939
private SpringApplication application;
4040

@@ -50,6 +50,7 @@ void bootstrapsApplicationContext() {
5050
.run("--spring.config.import=classpath:application-bootstrap-registry-integration-tests.properties")) {
5151
LoaderHelper bean = context.getBean(TestConfigDataBootstrap.LoaderHelper.class);
5252
assertThat(bean).isNotNull();
53+
assertThat(bean.getBound()).isEqualTo("igotbound");
5354
assertThat(bean.getLocation().getResolverHelper().getLocation()).isEqualTo("testbootstrap:test");
5455
}
5556
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
import java.io.IOException;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.function.Supplier;
2223

2324
import org.springframework.boot.BootstrapContextClosedEvent;
2425
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
26+
import org.springframework.boot.context.properties.bind.Binder;
2527
import org.springframework.context.ApplicationListener;
2628
import org.springframework.core.env.MapPropertySource;
2729

2830
/**
2931
* Test classes used with
30-
* {@link ConfigDataEnvironmentPostProcessorBootstrapRegistryIntegrationTests} to show how
32+
* {@link ConfigDataEnvironmentPostProcessorBootstrapContextIntegrationTests} to show how
3133
* a bootstrap registry can be used. This example will create helper instances during
3234
* result and load. It also shows how the helper can ultimately be registered as a bean.
3335
*
@@ -57,7 +59,7 @@ static class Loader implements ConfigDataLoader<Location> {
5759
@Override
5860
public ConfigData load(ConfigDataLoaderContext context, Location location) throws IOException {
5961
context.getBootstrapContext().registerIfAbsent(LoaderHelper.class,
60-
InstanceSupplier.from(() -> new LoaderHelper(location)));
62+
(bootstrapContext) -> new LoaderHelper(location, () -> bootstrapContext.get(Binder.class)));
6163
LoaderHelper helper = context.getBootstrapContext().get(LoaderHelper.class);
6264
context.getBootstrapContext().addCloseListener(helper);
6365
return new ConfigData(
@@ -103,14 +105,21 @@ static class LoaderHelper implements ApplicationListener<BootstrapContextClosedE
103105

104106
private final Location location;
105107

106-
LoaderHelper(Location location) {
108+
private final Supplier<Binder> binder;
109+
110+
LoaderHelper(Location location, Supplier<Binder> binder) {
107111
this.location = location;
112+
this.binder = binder;
108113
}
109114

110115
Location getLocation() {
111116
return this.location;
112117
}
113118

119+
String getBound() {
120+
return this.binder.get().bind("myprop", String.class).orElse(null);
121+
}
122+
114123
@Override
115124
public void onApplicationEvent(BootstrapContextClosedEvent event) {
116125
event.getApplicationContext().getBeanFactory().registerSingleton("loaderHelper", this);
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
spring.config.import=testbootstrap:test
1+
spring.config.import=testbootstrap:test
2+
myprop=igotbound

0 commit comments

Comments
 (0)