Migrate from deprecated Bootstrapper to BootstrapRegistryInitializer

Fixes gh-1890
This commit is contained in:
spencergibb
2021-05-24 13:02:10 -04:00
parent be708efc18
commit 42546c813e
6 changed files with 29 additions and 28 deletions

View File

@@ -17,7 +17,7 @@
package org.springframework.cloud.config.client;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.config.client.ConfigServerBootstrapper.LoaderInterceptor;
import org.springframework.retry.support.RetryTemplate;
@@ -29,13 +29,13 @@ import org.springframework.util.ClassUtils;
* @author Marcin Grzejszczak
* @since 3.0.0
*/
public class ConfigClientRetryBootstrapper implements Bootstrapper {
public class ConfigClientRetryBootstrapper implements BootstrapRegistryInitializer {
static final boolean RETRY_IS_PRESENT = ClassUtils.isPresent("org.springframework.retry.annotation.Retryable",
null);
@Override
public void intitialize(BootstrapRegistry registry) {
public void initialize(BootstrapRegistry registry) {
if (!RETRY_IS_PRESENT) {
return;
}

View File

@@ -22,14 +22,14 @@ import java.util.function.Function;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.BootstrapRegistry.InstanceSupplier;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataLoaderContext;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
public class ConfigServerBootstrapper implements Bootstrapper {
public class ConfigServerBootstrapper implements BootstrapRegistryInitializer {
private Function<BootstrapContext, RestTemplate> restTemplateFactory;
@@ -52,7 +52,7 @@ public class ConfigServerBootstrapper implements Bootstrapper {
}
@Override
public void intitialize(BootstrapRegistry registry) {
public void initialize(BootstrapRegistry registry) {
if (restTemplateFactory != null) {
registry.register(RestTemplate.class, restTemplateFactory::apply);
}

View File

@@ -21,6 +21,6 @@ org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver
org.springframework.boot.context.config.ConfigDataLoader=\
org.springframework.cloud.config.client.ConfigServerConfigDataLoader
# Spring Boot Bootstrappers
org.springframework.boot.Bootstrapper=\
# Spring Boot BootstrapRegistryInitializers
org.springframework.boot.BootstrapRegistryInitializer=\
org.springframework.cloud.config.client.ConfigClientRetryBootstrapper

View File

@@ -23,7 +23,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.BootstrapContext;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -54,10 +54,11 @@ public class ConfigServerConfigDataCustomizationIntegrationTests {
ConfigurableApplicationContext context = null;
try {
BindHandlerBootstrapper bindHandlerBootstrapper = new BindHandlerBootstrapper();
context = new SpringApplicationBuilder(TestConfig.class).addBootstrapper(bindHandlerBootstrapper)
.addBootstrapper(ConfigServerBootstrapper.create().withLoaderInterceptor(new Interceptor())
.withRestTemplateFactory(this::restTemplate))
.addBootstrapper(registry -> registry.addCloseListener(event -> {
context = new SpringApplicationBuilder(TestConfig.class)
.addBootstrapRegistryInitializer(bindHandlerBootstrapper)
.addBootstrapRegistryInitializer(ConfigServerBootstrapper.create()
.withLoaderInterceptor(new Interceptor()).withRestTemplateFactory(this::restTemplate))
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
BootstrapContext bootstrapContext = event.getBootstrapContext();
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();
@@ -153,12 +154,12 @@ public class ConfigServerConfigDataCustomizationIntegrationTests {
}
static class BindHandlerBootstrapper implements Bootstrapper {
static class BindHandlerBootstrapper implements BootstrapRegistryInitializer {
private int onSuccessCount = 0;
@Override
public void intitialize(BootstrapRegistry registry) {
public void initialize(BootstrapRegistry registry) {
registry.register(BindHandler.class, context -> new BindHandler() {
@Override
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,

View File

@@ -27,7 +27,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -104,19 +104,19 @@ public class DiscoveryClientConfigDataConfigurationNoRetryTests {
SpringApplicationBuilder builder = new SpringApplicationBuilder(TestConfig.class)
.properties(addDefaultEnv(env));
if (addInstanceProvider) {
builder.addBootstrapper(instanceProviderBootstrapper());
builder.addBootstrapRegistryInitializer(instanceProviderBootstrapper());
// ignore actual calls to config server since we're just testing discovery
// client.
builder.addBootstrapper(registry -> registry.register(ConfigServerBootstrapper.LoaderInterceptor.class,
ctx -> loadContext -> null));
builder.addBootstrapRegistryInitializer(registry -> registry
.register(ConfigServerBootstrapper.LoaderInterceptor.class, ctx -> loadContext -> null));
}
return builder.addBootstrapper(registry -> registry.addCloseListener(event -> {
return builder.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceMonitor monitor = event.getBootstrapContext().get(ConfigServerInstanceMonitor.class);
assertThat(monitor).as("ConfigServerInstanceMonitor was not created when it should").isNotNull();
}));
}
protected Bootstrapper instanceProviderBootstrapper() {
protected BootstrapRegistryInitializer instanceProviderBootstrapper() {
return registry -> registry.register(ConfigServerInstanceProvider.Function.class,
BootstrapRegistry.InstanceSupplier.from(() -> this.client::getInstances));
}

View File

@@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.Bootstrapper;
import org.springframework.boot.BootstrapRegistryInitializer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
@@ -67,7 +67,7 @@ public class DiscoveryClientConfigDataConfigurationTests {
public void offByDefault() {
context = new SpringApplicationBuilder(TestConfig.class)
.properties("spring.config.import=optional:configserver:")
.addBootstrapper(registry -> registry.addCloseListener(event -> {
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
try {
event.getBootstrapContext().get(ConfigServerInstanceMonitor.class);
fail("ConfigServerInstanceMonitor was created when it shouldn't");
@@ -217,19 +217,19 @@ public class DiscoveryClientConfigDataConfigurationTests {
SpringApplicationBuilder builder = new SpringApplicationBuilder(TestConfig.class)
.properties(addDefaultEnv(env));
if (addInstanceProvider) {
builder.addBootstrapper(instanceProviderBootstrapper());
builder.addBootstrapRegistryInitializer(instanceProviderBootstrapper());
// ignore actual calls to config server since we're just testing discovery
// client.
builder.addBootstrapper(registry -> registry.register(ConfigServerBootstrapper.LoaderInterceptor.class,
ctx -> loadContext -> null));
builder.addBootstrapRegistryInitializer(registry -> registry
.register(ConfigServerBootstrapper.LoaderInterceptor.class, ctx -> loadContext -> null));
}
return builder.addBootstrapper(registry -> registry.addCloseListener(event -> {
return builder.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
ConfigServerInstanceMonitor monitor = event.getBootstrapContext().get(ConfigServerInstanceMonitor.class);
assertThat(monitor).as("ConfigServerInstanceMonitor was not created when it should").isNotNull();
}));
}
protected Bootstrapper instanceProviderBootstrapper() {
protected BootstrapRegistryInitializer instanceProviderBootstrapper() {
return registry -> registry.register(ConfigServerInstanceProvider.Function.class,
BootstrapRegistry.InstanceSupplier.from(() -> this.client::getInstances));
}