Use ObjectProvider for DeferringLoadBalancerExchangeFilterFunction.

This commit is contained in:
Olga Maciaszek-Sharma
2024-07-30 15:37:14 +02:00
parent 30e0d173b8
commit 8fdd79f313
4 changed files with 87 additions and 8 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2012-2024 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
*
* https://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;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerWebClientBuilderBeanPostProcessor;
/**
* Wrapper for {@link ObjectProvider}. Added to use for a workaround in
* {@link LoadBalancerWebClientBuilderBeanPostProcessor}.
*
* @param <T> type of the object to fetch
* @author Spencer Gibb
* @deprecated for removal in 4.0
*/
@Deprecated(forRemoval = true)
public class SimpleObjectProvider<T> implements ObjectProvider<T> {
private final T object;
public SimpleObjectProvider(T object) {
this.object = object;
}
@Override
public T getObject(Object... args) throws BeansException {
return this.object;
}
@Override
public T getIfAvailable() throws BeansException {
return this.object;
}
@Override
public T getIfUnique() throws BeansException {
return this.object;
}
@Override
public T getObject() throws BeansException {
return this.object;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2024 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.
@@ -55,7 +55,7 @@ public class DeferringLoadBalancerExchangeFilterFunction<T extends ExchangeFilte
if (delegate == null) {
delegate = exchangeFilterFunctionProvider.getIfAvailable();
if (delegate == null) {
throw new IllegalStateException("ReactorLoadBalancerExchangeFilterFunction not available.");
throw new IllegalStateException("LoadBalancerExchangeFilterFunction not available.");
}
}
}

View File

@@ -27,7 +27,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.web.reactive.function.client.WebClient;
@@ -48,9 +47,10 @@ import org.springframework.web.reactive.function.client.WebClient;
@Conditional(LoadBalancerBeanPostProcessorAutoConfiguration.OnAnyLoadBalancerImplementationPresentCondition.class)
public class LoadBalancerBeanPostProcessorAutoConfiguration {
@SuppressWarnings("rawtypes")
@Bean
public static LoadBalancerWebClientBuilderBeanPostProcessor loadBalancerWebClientBuilderBeanPostProcessor(
@Lazy DeferringLoadBalancerExchangeFilterFunction deferringExchangeFilterFunction,
ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> deferringExchangeFilterFunction,
ApplicationContext context) {
return new LoadBalancerWebClientBuilderBeanPostProcessor(deferringExchangeFilterFunction, context);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2024 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.
@@ -17,8 +17,10 @@
package org.springframework.cloud.client.loadbalancer.reactive;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.SimpleObjectProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.web.reactive.function.client.WebClient;
@@ -30,15 +32,27 @@ import org.springframework.web.reactive.function.client.WebClient;
* @author Olga Maciaszek-Sharma
* @since 2.2.0
*/
@SuppressWarnings({ "removal", "rawtypes" })
public class LoadBalancerWebClientBuilderBeanPostProcessor implements BeanPostProcessor {
private final DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction;
private final ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> exchangeFilterFunctionObjectProvider;
private final ApplicationContext context;
/**
* @deprecated in favour of {@link LoadBalancerWebClientBuilderBeanPostProcessor#LoadBalancerWebClientBuilderBeanPostProcessor(ObjectProvider, ApplicationContext)}
*/
@Deprecated(forRemoval = true)
public LoadBalancerWebClientBuilderBeanPostProcessor(
DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction, ApplicationContext context) {
this.exchangeFilterFunction = exchangeFilterFunction;
this.exchangeFilterFunctionObjectProvider = new SimpleObjectProvider<>(exchangeFilterFunction);
this.context = context;
}
public LoadBalancerWebClientBuilderBeanPostProcessor(
ObjectProvider<DeferringLoadBalancerExchangeFilterFunction> exchangeFilterFunction,
ApplicationContext context) {
this.exchangeFilterFunctionObjectProvider = exchangeFilterFunction;
this.context = context;
}
@@ -48,7 +62,12 @@ public class LoadBalancerWebClientBuilderBeanPostProcessor implements BeanPostPr
if (context.findAnnotationOnBean(beanName, LoadBalanced.class) == null) {
return bean;
}
((WebClient.Builder) bean).filter(exchangeFilterFunction);
DeferringLoadBalancerExchangeFilterFunction exchangeFilterFunction = exchangeFilterFunctionObjectProvider
.getIfAvailable();
if (exchangeFilterFunction == null) {
throw new IllegalStateException("LoadBalancerExchangeFilterFunction not found");
}
((WebClient.Builder) bean).filter(exchangeFilterFunctionObjectProvider.getIfAvailable());
}
return bean;
}