diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java index 44fd99c8..be28a4a0 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfiguration.java @@ -4,7 +4,6 @@ import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.context.annotation.Bean; diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/WebClientCustomizer.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/WebClientCustomizer.java new file mode 100644 index 00000000..0bcb3083 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/WebClientCustomizer.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.client.loadbalancer.reactive; + +import org.springframework.web.reactive.function.client.WebClient; + +/** + * Callback interface that can be used to customize a + * {@link org.springframework.web.reactive.function.client.WebClient.Builder + * WebClient.Builder}. + * + * See original {@link org.springframework.boot.web.reactive.function.client.WebClientCustomizer} + * + * @author Brian Clozel + * @since 2.1.0 + */ +@FunctionalInterface +public interface WebClientCustomizer { + + /** + * Callback to customize a + * {@link org.springframework.web.reactive.function.client.WebClient.Builder + * WebClient.Builder} instance. + * @param webClientBuilder the client builder to customize + */ + void customize(WebClient.Builder webClientBuilder); + +} + diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java index 101931bf..9cb3cf21 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/reactive/ReactiveLoadBalancerAutoConfigurationTests.java @@ -16,9 +16,17 @@ package org.springframework.cloud.client.loadbalancer.reactive; +import java.io.IOException; +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.Random; + import org.junit.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; @@ -34,12 +42,6 @@ import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.WebClient; -import java.io.IOException; -import java.net.URI; -import java.util.List; -import java.util.Map; -import java.util.Random; - import static org.assertj.core.api.Assertions.assertThat; @@ -90,20 +92,29 @@ public class ReactiveLoadBalancerAutoConfigurationTests { assertThat(getFilters(two.nonLoadBalanced)).isNullOrEmpty(); } + @Test + public void noCustomWebClientBuilders() { + ConfigurableApplicationContext context = init(NoWebClientBuilder.class); + final Map webClientBuilders = context + .getBeansOfType(WebClient.Builder.class); + + assertThat(webClientBuilders).hasSize(1); + + WebClient.Builder builder = context.getBean(WebClient.Builder.class); + + assertThat(builder).isNotNull(); + assertThat(getFilters(builder)).isNullOrEmpty(); + } + protected ConfigurableApplicationContext init(Class config) { return new SpringApplicationBuilder().web(WebApplicationType.NONE) // .properties("spring.aop.proxyTargetClass=true") - .sources(config, ReactiveLoadBalancerAutoConfiguration.class).run(); + .sources(config, WebClientAutoConfiguration.class, + ReactiveLoadBalancerAutoConfiguration.class).run(); } @Configuration - protected static class OneWebClientBuilder { - - @Bean - @LoadBalanced - WebClient.Builder loadBalancedWebClientBuilder() { - return WebClient.builder(); - } + protected static class NoWebClientBuilder { @Bean LoadBalancerClient loadBalancerClient() { @@ -116,23 +127,23 @@ public class ReactiveLoadBalancerAutoConfigurationTests { } @Configuration - protected static class TwoWebClientBuilders { + protected static class OneWebClientBuilder extends NoWebClientBuilder { - @Primary @Bean - WebClient.Builder webClientBuilder() { - return WebClient.builder(); - } - @LoadBalanced - @Bean WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } + } + + @Configuration + protected static class TwoWebClientBuilders extends OneWebClientBuilder { + + @Primary @Bean - LoadBalancerClient loadBalancerClient() { - return new NoopLoadBalancerClient(); + WebClient.Builder webClientBuilder() { + return WebClient.builder(); } @Configuration