Change scope of bootstrap registered Binder

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
pull/24597/head
Phillip Webb 4 years ago
parent e1b158ec66
commit 5317d8a9bb

@ -22,10 +22,12 @@ import java.util.Collections;
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;
@ -220,11 +222,10 @@ class ConfigDataEnvironment {
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);
@ -232,6 +233,10 @@ class ConfigDataEnvironment {
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");

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

Loading…
Cancel
Save