Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.Ordered;
Expand Down Expand Up @@ -61,15 +62,22 @@ public int getOrder() {
return -1;
}

protected ConfigClientProperties loadProperties(Binder binder) {
protected ConfigClientProperties loadProperties(ConfigDataLocationResolverContext context) {
Binder binder = context.getBinder();
BindHandler bindHandler = getBindHandler(context);
ConfigClientProperties configClientProperties = binder
.bind(ConfigClientProperties.PREFIX, Bindable.of(ConfigClientProperties.class))
.orElse(new ConfigClientProperties());
String applicationName = binder.bind("spring.application.name", String.class).orElse("application");
.bind(ConfigClientProperties.PREFIX, Bindable.of(ConfigClientProperties.class), bindHandler)
.orElseGet(ConfigClientProperties::new);
String applicationName = binder.bind("spring.application.name", Bindable.of(String.class), bindHandler)
.orElse("application");
configClientProperties.setName(applicationName);
return configClientProperties;
}

private BindHandler getBindHandler(ConfigDataLocationResolverContext context) {
return context.getBootstrapContext().getOrElse(BindHandler.class, null);
}

protected RestTemplate createRestTemplate(ConfigClientProperties properties) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
if (properties.getRequestReadTimeout() < 0) {
Expand Down Expand Up @@ -120,8 +128,7 @@ public List<ConfigServerConfigDataResource> resolve(ConfigDataLocationResolverCo
public List<ConfigServerConfigDataResource> resolveProfileSpecific(
ConfigDataLocationResolverContext resolverContext, ConfigDataLocation location, Profiles profiles)
throws ConfigDataLocationNotFoundException {
ConfigClientProperties properties = loadProperties(resolverContext.getBinder());

ConfigClientProperties properties = loadProperties(resolverContext);
String uris = location.getNonPrefixedValue(getPrefix());

if (StringUtils.hasText(uris)) {
Expand All @@ -139,7 +146,8 @@ public List<ConfigServerConfigDataResource> resolveProfileSpecific(
return createRestTemplate(props);
});

boolean discoveryEnabled = resolverContext.getBinder().bind(CONFIG_DISCOVERY_ENABLED, Boolean.class)
boolean discoveryEnabled = resolverContext.getBinder()
.bind(CONFIG_DISCOVERY_ENABLED, Bindable.of(Boolean.class), getBindHandler(resolverContext))
.orElse(false);

if (discoveryEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@

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.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.properties.bind.BindContext;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.cloud.config.client.ConfigServerBootstrapper.LoaderInterceptor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.client.RestTemplate;
Expand All @@ -37,7 +43,8 @@ public class ConfigServerConfigDataCustomizationIntegrationTests {
void customizableRestTemplate() {
ConfigurableApplicationContext context = null;
try {
context = new SpringApplicationBuilder(TestConfig.class)
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 -> {
Expand All @@ -47,7 +54,8 @@ void customizableRestTemplate() {
RestTemplate restTemplate = bootstrapContext.get(RestTemplate.class);
beanFactory.registerSingleton("holder", new RestTemplateHolder(restTemplate));
beanFactory.registerSingleton("interceptor", bootstrapContext.get(LoaderInterceptor.class));
})).run("--spring.config.import=optional:configserver:", "--custom.prop=customval");
})).run("--spring.config.import=optional:configserver:", "--custom.prop=customval",
"--spring.cloud.config.label=mylabel");

RestTemplateHolder holder = context.getBean(RestTemplateHolder.class);
assertThat(holder).isNotNull();
Expand All @@ -60,6 +68,8 @@ void customizableRestTemplate() {
Interceptor interceptor = (Interceptor) loaderInterceptor;
assertThat(interceptor.applied).isTrue();
assertThat(interceptor.hasBinder).isTrue();

assertThat(bindHandlerBootstrapper.onSuccessCount).isGreaterThan(1);
}
finally {
if (context != null) {
Expand Down Expand Up @@ -115,4 +125,22 @@ static class CustomRestTemplate extends RestTemplate {

}

static class BindHandlerBootstrapper implements Bootstrapper {

private int onSuccessCount = 0;

@Override
public void intitialize(BootstrapRegistry registry) {
registry.register(BindHandler.class, context -> new BindHandler() {
@Override
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
Object result) {
onSuccessCount++;
return result;
}
});
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

public class ApplicationFailFastTests {

// FIXME: configdata failfast works.
@Test
public void contextFails() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
// Normally spring.cloud.config.enabled:true is the default but since we have the
// config
// server on the classpath we need to set it explicitly
// config server on the classpath we need to set it explicitly
properties = { "spring.cloud.config.enabled:true",
// FIXME: configdata why is this needed here?
"spring.config.use-legacy-processing=true", "management.security.enabled=false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@
*
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(KeyProperties.class)
@EnableConfigurationProperties
@Import({ SingleTextEncryptorConfiguration.class, DefaultTextEncryptorConfiguration.class })
public class EncryptionAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public KeyProperties keyProperties() {
return new KeyProperties();
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.config.server.encrypt.enabled", matchIfMissing = true)
protected static class EncryptorConfiguration {
Expand Down