Skip to content

Commit

Permalink
Change scope of bootstrap registered Binder
Browse files Browse the repository at this point in the history
Update `ConfigDataEnvironment` so that the `Binder` is registered as
a prototype bootstrap instance. This allows it to be accessed early
but still replaced when a more complete version is available.

Fixes gh-24559
  • Loading branch information
philwebb committed Dec 18, 2020
1 parent e1b158e commit 5317d8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Expand Up @@ -22,10 +22,12 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import org.apache.commons.logging.Log;

import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.BootstrapRegistry.Scope;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.DefaultPropertiesPropertySource;
import org.springframework.boot.context.config.ConfigDataEnvironmentContributors.BinderOption;
Expand Down Expand Up @@ -220,18 +222,21 @@ private ConfigDataEnvironmentContributor createInitialImportContributor(ConfigDa
void processAndApply() {
ConfigDataImporter importer = new ConfigDataImporter(this.logFactory, this.notFoundAction, this.resolvers,
this.loaders);
this.bootstrapContext.register(Binder.class, InstanceSupplier
.from(() -> this.contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE)));
registerBootstrapBinder(() -> this.contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE));
ConfigDataEnvironmentContributors contributors = processInitial(this.contributors, importer);
Binder initialBinder = contributors.getBinder(null, BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
this.bootstrapContext.register(Binder.class, InstanceSupplier.of(initialBinder));
registerBootstrapBinder(() -> initialBinder);
ConfigDataActivationContext activationContext = createActivationContext(initialBinder);
contributors = processWithoutProfiles(contributors, importer, activationContext);
activationContext = withProfiles(contributors, activationContext);
contributors = processWithProfiles(contributors, importer, activationContext);
applyToEnvironment(contributors, activationContext);
}

private void registerBootstrapBinder(Supplier<Binder> supplier) {
this.bootstrapContext.register(Binder.class, InstanceSupplier.from(supplier).withScope(Scope.PROTOTYPE));
}

private ConfigDataEnvironmentContributors processInitial(ConfigDataEnvironmentContributors contributors,
ConfigDataImporter importer) {
this.logger.trace("Processing initial config data environment contributors without activation context");
Expand Down
Expand Up @@ -41,6 +41,7 @@ static class LocationResolver implements ConfigDataLocationResolver<Resource> {

@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
context.getBootstrapContext().get(Binder.class); // gh-24559
return location.hasPrefix("testbootstrap:");
}

Expand Down

0 comments on commit 5317d8a

Please sign in to comment.