Skip to content

Commit 8fdd79f

Browse files
committed
Use ObjectProvider for DeferringLoadBalancerExchangeFilterFunction.
1 parent 30e0d17 commit 8fdd79f

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.client.loadbalancer;
18+
19+
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.ObjectProvider;
21+
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerWebClientBuilderBeanPostProcessor;
22+
23+
/**
24+
* Wrapper for {@link ObjectProvider}. Added to use for a workaround in
25+
* {@link LoadBalancerWebClientBuilderBeanPostProcessor}.
26+
*
27+
* @param <T> type of the object to fetch
28+
* @author Spencer Gibb
29+
* @deprecated for removal in 4.0
30+
*/
31+
@Deprecated(forRemoval = true)
32+
public class SimpleObjectProvider<T> implements ObjectProvider<T> {
33+
34+
private final T object;
35+
36+
public SimpleObjectProvider(T object) {
37+
this.object = object;
38+
}
39+
40+
@Override
41+
public T getObject(Object... args) throws BeansException {
42+
return this.object;
43+
}
44+
45+
@Override
46+
public T getIfAvailable() throws BeansException {
47+
return this.object;
48+
}
49+
50+
@Override
51+
public T getIfUnique() throws BeansException {
52+
return this.object;
53+
}
54+
55+
@Override
56+
public T getObject() throws BeansException {
57+
return this.object;
58+
}
59+
60+
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DeferringLoadBalancerExchangeFilterFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ void tryResolveDelegate() {
5555
if (delegate == null) {
5656
delegate = exchangeFilterFunctionProvider.getIfAvailable();
5757
if (delegate == null) {
58-
throw new IllegalStateException("ReactorLoadBalancerExchangeFilterFunction not available.");
58+
throw new IllegalStateException("LoadBalancerExchangeFilterFunction not available.");
5959
}
6060
}
6161
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerBeanPostProcessorAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Conditional;
2929
import org.springframework.context.annotation.Configuration;
30-
import org.springframework.context.annotation.Lazy;
3130
import org.springframework.context.annotation.Primary;
3231
import org.springframework.web.reactive.function.client.WebClient;
3332

@@ -48,9 +47,10 @@
4847
@Conditional(LoadBalancerBeanPostProcessorAutoConfiguration.OnAnyLoadBalancerImplementationPresentCondition.class)
4948
public class LoadBalancerBeanPostProcessorAutoConfiguration {
5049

50+
@SuppressWarnings("rawtypes")
5151
@Bean
5252
public static LoadBalancerWebClientBuilderBeanPostProcessor loadBalancerWebClientBuilderBeanPostProcessor(
53-
@Lazy DeferringLoadBalancerExchangeFilterFunction deferringExchangeFilterFunction,
53+
ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> deferringExchangeFilterFunction,
5454
ApplicationContext context) {
5555
return new LoadBalancerWebClientBuilderBeanPostProcessor(deferringExchangeFilterFunction, context);
5656
}

spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerWebClientBuilderBeanPostProcessor.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,8 +17,10 @@
1717
package org.springframework.cloud.client.loadbalancer.reactive;
1818

1919
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.ObjectProvider;
2021
import org.springframework.beans.factory.config.BeanPostProcessor;
2122
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
23+
import org.springframework.cloud.client.loadbalancer.SimpleObjectProvider;
2224
import org.springframework.context.ApplicationContext;
2325
import org.springframework.web.reactive.function.client.WebClient;
2426

@@ -30,15 +32,27 @@
3032
* @author Olga Maciaszek-Sharma
3133
* @since 2.2.0
3234
*/
35+
@SuppressWarnings({ "removal", "rawtypes" })
3336
public class LoadBalancerWebClientBuilderBeanPostProcessor implements BeanPostProcessor {
3437

35-
private final DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction;
38+
private final ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> exchangeFilterFunctionObjectProvider;
3639

3740
private final ApplicationContext context;
3841

42+
/**
43+
* @deprecated in favour of {@link LoadBalancerWebClientBuilderBeanPostProcessor#LoadBalancerWebClientBuilderBeanPostProcessor(ObjectProvider, ApplicationContext)}
44+
*/
45+
@Deprecated(forRemoval = true)
3946
public LoadBalancerWebClientBuilderBeanPostProcessor(
4047
DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction, ApplicationContext context) {
41-
this.exchangeFilterFunction = exchangeFilterFunction;
48+
this.exchangeFilterFunctionObjectProvider = new SimpleObjectProvider<>(exchangeFilterFunction);
49+
this.context = context;
50+
}
51+
52+
public LoadBalancerWebClientBuilderBeanPostProcessor(
53+
ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> exchangeFilterFunction,
54+
ApplicationContext context) {
55+
this.exchangeFilterFunctionObjectProvider = exchangeFilterFunction;
4256
this.context = context;
4357
}
4458

@@ -48,7 +62,12 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro
4862
if (context.findAnnotationOnBean(beanName, LoadBalanced.class) == null) {
4963
return bean;
5064
}
51-
((WebClient.Builder) bean).filter(exchangeFilterFunction);
65+
DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction = exchangeFilterFunctionObjectProvider
66+
.getIfAvailable();
67+
if (exchangeFilterFunction == null) {
68+
throw new IllegalStateException("LoadBalancerExchangeFilterFunction not found");
69+
}
70+
((WebClient.Builder) bean).filter(exchangeFilterFunctionObjectProvider.getIfAvailable());
5271
}
5372
return bean;
5473
}

0 commit comments

Comments
 (0)