Skip to content

Commit 0883162

Browse files
committed
Add BeanFactory-based implementation for WebClient.
1 parent eaedef4 commit 0883162

File tree

5 files changed

+43
-148
lines changed

5 files changed

+43
-148
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/InterfaceClientsAdapter.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/HttpInterfaceClientsAdapter.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/interfaceclients/http/HttpInterfaceClientsAutoConfiguration.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.autoconfigure.AutoConfiguration;
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2223
import org.springframework.boot.autoconfigure.web.client.NotReactiveWebApplicationCondition;
2324
import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
@@ -31,6 +32,8 @@
3132
import org.springframework.web.client.RestTemplate;
3233
import org.springframework.web.client.support.RestClientAdapter;
3334
import org.springframework.web.client.support.RestTemplateAdapter;
35+
import org.springframework.web.reactive.function.client.WebClient;
36+
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
3437
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
3538

3639
/**
@@ -61,21 +64,12 @@ protected static class RestTemplateAdapterProviderConfiguration {
6164

6265
}
6366

64-
// @Configuration(proxyBeanMethods = false)
65-
// @ConditionalOnClass({ WebClient.class, WebClientAdapter.class,
66-
// HttpServiceProxyFactory.class })
67-
// @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
68-
// protected static class WebClientAdapterProviderConfiguration {
69-
//
70-
// @Bean
71-
// @ConditionalOnBean(WebClient.Builder.class)
72-
// @ConditionalOnMissingBean
73-
// HttpExchangeAdapterProvider webClientAdapterProvider(WebClient.Builder
74-
// webClientBuilder,
75-
// ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider) {
76-
// return new WebClientAdapterProvider(webClientBuilder, propertiesProvider);
77-
// }
78-
//
79-
// }
67+
@Configuration(proxyBeanMethods = false)
68+
@ConditionalOnClass({ WebClient.class, WebClientAdapter.class, HttpServiceProxyFactory.class })
69+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
70+
@Import(WebClientInterfaceClientsImportRegistrar.class)
71+
protected static class WebClientAdapterProviderConfiguration {
72+
73+
}
8074

8175
}
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,48 @@
1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
2121

22-
import org.springframework.beans.factory.ListableBeanFactory;
23-
import org.springframework.beans.factory.ObjectProvider;
24-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2522
import org.springframework.web.reactive.function.client.WebClient;
2623
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
2724
import org.springframework.web.service.invoker.HttpExchangeAdapter;
2825

2926
/**
3027
* @author Olga Maciaszek-Sharma
3128
*/
32-
public class WebClientAdapterProvider implements HttpExchangeAdapterProvider {
29+
public class WebClientInterfaceClientsFactoryBean extends AbstractHttpInterfaceClientsFactoryBean {
3330

34-
private static final Log logger = LogFactory.getLog(WebClientAdapterProvider.class);
35-
36-
private final WebClient.Builder builder;
37-
38-
private final ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider;
39-
40-
public WebClientAdapterProvider(WebClient.Builder builder,
41-
ObjectProvider<HttpInterfaceClientsProperties> propertiesProvider) {
42-
this.builder = builder;
43-
this.propertiesProvider = propertiesProvider;
44-
}
31+
private static final Log logger = LogFactory.getLog(WebClientInterfaceClientsFactoryBean.class);
4532

4633
@Override
47-
public HttpExchangeAdapter get(ConfigurableListableBeanFactory beanFactory, String clientId) {
48-
WebClient userProvidedWebClient = QualifiedBeanProvider.qualifiedBean(beanFactory, WebClient.class, clientId);
34+
protected HttpExchangeAdapter exchangeAdapter() {
35+
String baseUrl = getBaseUrl();
36+
37+
WebClient userProvidedWebClient = QualifiedBeanProvider.qualifiedBean(this.applicationContext.getBeanFactory(),
38+
WebClient.class, this.clientId);
4939
if (userProvidedWebClient != null) {
40+
// If the user wants to set the baseUrl directly on the builder,
41+
// it should not be set in properties.
42+
if (baseUrl != null) {
43+
userProvidedWebClient = userProvidedWebClient.mutate().baseUrl(baseUrl).build();
44+
}
5045
return WebClientAdapter.create(userProvidedWebClient);
5146
}
52-
HttpInterfaceClientsProperties properties = this.propertiesProvider.getObject();
53-
String baseUrl = properties.getProperties(clientId).getBaseUrl();
54-
WebClient.Builder userProvidedWebClientBuilder = QualifiedBeanProvider.qualifiedBean(beanFactory,
55-
WebClient.Builder.class, clientId);
47+
48+
WebClient.Builder userProvidedWebClientBuilder = QualifiedBeanProvider
49+
.qualifiedBean(this.applicationContext.getBeanFactory(), WebClient.Builder.class, this.clientId);
5650
if (userProvidedWebClientBuilder != null) {
57-
// If the user wants to set the baseUrl directly on the builder,
58-
// it should not be set in properties.
51+
5952
if (baseUrl != null) {
6053
userProvidedWebClientBuilder.baseUrl(baseUrl);
6154
}
6255
return WebClientAdapter.create(userProvidedWebClientBuilder.build());
6356
}
57+
6458
// create a WebClientAdapter bean with default implementation
6559
if (logger.isDebugEnabled()) {
66-
logger.debug("Creating WebClientAdapter for '" + clientId + "'");
60+
logger.debug("Creating WebClientAdapter for '" + this.clientId + "'");
6761
}
68-
WebClient webClient = this.builder.baseUrl(baseUrl).build();
62+
WebClient.Builder builder = this.applicationContext.getBean(WebClient.Builder.class);
63+
WebClient webClient = builder.baseUrl(baseUrl).build();
6964
return WebClientAdapter.create(webClient);
7065
}
7166

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616

1717
package org.springframework.boot.autoconfigure.interfaceclients.http;
1818

19-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
20-
import org.springframework.web.service.invoker.HttpExchangeAdapter;
19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.autoconfigure.interfaceclients.AbstractInterfaceClientsImportRegistrar;
2122

2223
/**
2324
* @author Olga Maciaszek-Sharma
2425
*/
25-
@FunctionalInterface
26-
public interface HttpExchangeAdapterProvider {
26+
public class WebClientInterfaceClientsImportRegistrar extends AbstractInterfaceClientsImportRegistrar {
27+
28+
@Override
29+
protected Class<? extends Annotation> getAnnotation() {
30+
return HttpClient.class;
31+
}
2732

28-
// TODO: try a less specific type for the beanFactory
29-
HttpExchangeAdapter get(ConfigurableListableBeanFactory beanFactory, String clientId);
33+
@Override
34+
protected Class<?> getFactoryBeanClass() {
35+
return WebClientInterfaceClientsFactoryBean.class;
36+
}
3037

3138
}

0 commit comments

Comments
 (0)