From f92bdf7df5d8523e71039519ae4bc00f617ffdd0 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Wed, 31 Jul 2024 17:05:27 +0200 Subject: [PATCH] Reapply "Use ObjectProvider for DeferringLoadBalancerExchangeFilterFunction." This reverts commit c1b08f8b9bcb9a762218b78b9416f6b7ab3fe31a. --- .../loadbalancer/SimpleObjectProvider.java | 60 +++++++++++++++++++ ...ingLoadBalancerExchangeFilterFunction.java | 4 +- ...cerBeanPostProcessorAutoConfiguration.java | 4 +- ...ncerWebClientBuilderBeanPostProcessor.java | 28 +++++++-- 4 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/SimpleObjectProvider.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/SimpleObjectProvider.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/SimpleObjectProvider.java new file mode 100644 index 00000000..b12ce977 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/SimpleObjectProvider.java @@ -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 type of the object to fetch + * @author Spencer Gibb + * @deprecated for removal in 4.0 + */ +@Deprecated(forRemoval = true) +public class SimpleObjectProvider implements ObjectProvider { + + 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; + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DeferringLoadBalancerExchangeFilterFunction.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DeferringLoadBalancerExchangeFilterFunction.java index 4e419770..d370b981 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DeferringLoadBalancerExchangeFilterFunction.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/DeferringLoadBalancerExchangeFilterFunction.java @@ -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 deferringExchangeFilterFunction, ApplicationContext context) { return new LoadBalancerWebClientBuilderBeanPostProcessor(deferringExchangeFilterFunction, context); } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerWebClientBuilderBeanPostProcessor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerWebClientBuilderBeanPostProcessor.java index bb8db3ff..e07f9590 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerWebClientBuilderBeanPostProcessor.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/reactive/LoadBalancerWebClientBuilderBeanPostProcessor.java @@ -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,28 @@ 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 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 exchangeFilterFunction, + ApplicationContext context) { + this.exchangeFilterFunctionObjectProvider = exchangeFilterFunction; this.context = context; } @@ -48,7 +63,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; }