Skip to content

Commit 66ad4bf

Browse files
committed
Adds test that a BindHandler in bootstrap context is used.
1 parent 4171b73 commit 66ad4bf

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLocationResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected ConfigClientProperties loadProperties(ConfigDataLocationResolverContex
6767
BindHandler bindHandler = getBindHandler(context);
6868
ConfigClientProperties configClientProperties = binder
6969
.bind(ConfigClientProperties.PREFIX, Bindable.of(ConfigClientProperties.class), bindHandler)
70-
.orElse(new ConfigClientProperties());
70+
.orElseGet(ConfigClientProperties::new);
7171
String applicationName = binder.bind("spring.application.name", Bindable.of(String.class), bindHandler)
7272
.orElse("application");
7373
configClientProperties.setName(applicationName);

spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerConfigDataCustomizationIntegrationTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020

2121
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2222
import org.springframework.boot.BootstrapContext;
23+
import org.springframework.boot.BootstrapRegistry;
24+
import org.springframework.boot.Bootstrapper;
2325
import org.springframework.boot.SpringBootConfiguration;
2426
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2527
import org.springframework.boot.builder.SpringApplicationBuilder;
2628
import org.springframework.boot.context.config.ConfigData;
29+
import org.springframework.boot.context.properties.bind.BindContext;
30+
import org.springframework.boot.context.properties.bind.BindHandler;
31+
import org.springframework.boot.context.properties.bind.Bindable;
2732
import org.springframework.boot.context.properties.bind.Binder;
33+
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
2834
import org.springframework.cloud.config.client.ConfigServerBootstrapper.LoaderInterceptor;
2935
import org.springframework.context.ConfigurableApplicationContext;
3036
import org.springframework.web.client.RestTemplate;
@@ -37,7 +43,9 @@ public class ConfigServerConfigDataCustomizationIntegrationTests {
3743
void customizableRestTemplate() {
3844
ConfigurableApplicationContext context = null;
3945
try {
46+
BindHandlerBootstrapper bindHandlerBootstrapper = new BindHandlerBootstrapper();
4047
context = new SpringApplicationBuilder(TestConfig.class)
48+
.addBootstrapper(bindHandlerBootstrapper)
4149
.addBootstrapper(ConfigServerBootstrapper.create().withLoaderInterceptor(new Interceptor())
4250
.withRestTemplateFactory(this::restTemplate))
4351
.addBootstrapper(registry -> registry.addCloseListener(event -> {
@@ -47,7 +55,7 @@ void customizableRestTemplate() {
4755
RestTemplate restTemplate = bootstrapContext.get(RestTemplate.class);
4856
beanFactory.registerSingleton("holder", new RestTemplateHolder(restTemplate));
4957
beanFactory.registerSingleton("interceptor", bootstrapContext.get(LoaderInterceptor.class));
50-
})).run("--spring.config.import=optional:configserver:", "--custom.prop=customval");
58+
})).run("--spring.config.import=optional:configserver:", "--custom.prop=customval", "--spring.cloud.config.label=mylabel");
5159

5260
RestTemplateHolder holder = context.getBean(RestTemplateHolder.class);
5361
assertThat(holder).isNotNull();
@@ -60,6 +68,8 @@ void customizableRestTemplate() {
6068
Interceptor interceptor = (Interceptor) loaderInterceptor;
6169
assertThat(interceptor.applied).isTrue();
6270
assertThat(interceptor.hasBinder).isTrue();
71+
72+
assertThat(bindHandlerBootstrapper.onSuccessCount).isGreaterThan(1);
6373
}
6474
finally {
6575
if (context != null) {
@@ -115,4 +125,20 @@ static class CustomRestTemplate extends RestTemplate {
115125

116126
}
117127

128+
static class BindHandlerBootstrapper implements Bootstrapper {
129+
130+
private int onSuccessCount = 0;
131+
132+
@Override
133+
public void intitialize(BootstrapRegistry registry) {
134+
registry.register(BindHandler.class, context -> new BindHandler() {
135+
@Override
136+
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
137+
onSuccessCount++;
138+
return result;
139+
}
140+
});
141+
}
142+
}
143+
118144
}

0 commit comments

Comments
 (0)