From 213f281523971e446b5e24f5e9ea4bc524708646 Mon Sep 17 00:00:00 2001 From: Olga MaciaszekSharma Date: Tue, 16 Jan 2024 11:30:10 +0100 Subject: [PATCH] Add back LoadBalancerRestClientBuilderBeanPostProcessor. --- ...cerRestClientBuilderBeanPostProcessor.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRestClientBuilderBeanPostProcessor.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRestClientBuilderBeanPostProcessor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRestClientBuilderBeanPostProcessor.java new file mode 100644 index 00000000..5d5bf4c9 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRestClientBuilderBeanPostProcessor.java @@ -0,0 +1,57 @@ +/* + * 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.config.BeanPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.web.client.RestClient; + +/** + * A {@link BeanPostProcessor} that adds the provided {@link ClientHttpRequestInterceptor} + * to all {@link RestClient.Builder} instances annotated with {@link LoadBalanced}. + * + * @author Olga Maciaszek-Sharma + * @since 4.1.0 + * @deprecated to be removed in the next release. + */ +@Deprecated +public class LoadBalancerRestClientBuilderBeanPostProcessor implements BeanPostProcessor { + + private final ClientHttpRequestInterceptor loadBalancerInterceptor; + + private final ApplicationContext context; + + public LoadBalancerRestClientBuilderBeanPostProcessor(ClientHttpRequestInterceptor loadBalancerInterceptor, + ApplicationContext context) { + this.loadBalancerInterceptor = loadBalancerInterceptor; + this.context = context; + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof RestClient.Builder) { + if (context.findAnnotationOnBean(beanName, LoadBalanced.class) == null) { + return bean; + } + ((RestClient.Builder) bean).requestInterceptor(loadBalancerInterceptor); + } + return bean; + } + +}