diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java index 8232314a9d..2cbf0faa85 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java @@ -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 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"); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/TestConfigDataBootstrap.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/TestConfigDataBootstrap.java index e3e0be4f22..e61d935994 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/TestConfigDataBootstrap.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/TestConfigDataBootstrap.java @@ -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:"); }