2020
2121import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
2222import org .springframework .boot .BootstrapContext ;
23+ import org .springframework .boot .BootstrapRegistry ;
24+ import org .springframework .boot .Bootstrapper ;
2325import org .springframework .boot .SpringBootConfiguration ;
2426import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
2527import org .springframework .boot .builder .SpringApplicationBuilder ;
2628import 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 ;
2732import org .springframework .boot .context .properties .bind .Binder ;
33+ import org .springframework .boot .context .properties .source .ConfigurationPropertyName ;
2834import org .springframework .cloud .config .client .ConfigServerBootstrapper .LoaderInterceptor ;
2935import org .springframework .context .ConfigurableApplicationContext ;
3036import 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