|
16 | 16 |
|
17 | 17 | package org.springframework.cloud.client.loadbalancer; |
18 | 18 |
|
19 | | -import org.springframework.beans.BeansException; |
20 | 19 | import org.springframework.beans.factory.ObjectProvider; |
21 | | -import org.springframework.beans.factory.config.BeanPostProcessor; |
22 | 20 | import org.springframework.context.ApplicationContext; |
23 | 21 | import org.springframework.http.client.ClientHttpRequestInterceptor; |
24 | 22 | import org.springframework.web.client.RestClient; |
25 | 23 |
|
26 | 24 | /** |
27 | | - * A {@link BeanPostProcessor} that adds the provided {@link ClientHttpRequestInterceptor} |
28 | | - * to all {@link RestClient.Builder} instances annotated with {@link LoadBalanced}. |
| 25 | + * {@link RestClient.Builder}-specific |
| 26 | + * {@link AbstractLoadBalancerBlockingBuilderBeanPostProcessor} implementation. Adds the |
| 27 | + * provided {@link ClientHttpRequestInterceptor} to all {@link RestClient.Builder} |
| 28 | + * instances annotated with {@link LoadBalanced}. |
29 | 29 | * |
30 | 30 | * @author Olga Maciaszek-Sharma |
31 | 31 | * @since 4.1.0 |
32 | 32 | */ |
33 | 33 | public class LoadBalancerRestClientBuilderBeanPostProcessor<T extends ClientHttpRequestInterceptor> |
34 | | - implements BeanPostProcessor { |
35 | | - |
36 | | - private final ObjectProvider<T> loadBalancerInterceptorProvider; |
37 | | - |
38 | | - private final ApplicationContext context; |
| 34 | + extends AbstractLoadBalancerBlockingBuilderBeanPostProcessor<T> { |
39 | 35 |
|
40 | 36 | /** |
41 | | - * Creates a {@link LoadBalancerRestClientBuilderBeanPostProcessor} instance using a provided {@link ClientHttpRequestInterceptor} and application context. |
42 | | - * @param loadBalancerInterceptor a {@link ClientHttpRequestInterceptor} used for load-balancing |
| 37 | + * Creates a {@link LoadBalancerRestClientBuilderBeanPostProcessor} instance using a |
| 38 | + * provided {@link ClientHttpRequestInterceptor} and application context. |
| 39 | + * @param loadBalancerInterceptor a {@link ClientHttpRequestInterceptor} used for |
| 40 | + * load-balancing |
43 | 41 | * @param context {@link ApplicationContext} |
44 | | - * @deprecated in favour of {@link LoadBalancerRestClientBuilderBeanPostProcessor#LoadBalancerRestClientBuilderBeanPostProcessor(ObjectProvider, ApplicationContext)} |
| 42 | + * @deprecated in favour of |
| 43 | + * {@link LoadBalancerRestClientBuilderBeanPostProcessor#LoadBalancerRestClientBuilderBeanPostProcessor(ObjectProvider, ApplicationContext)} |
45 | 44 | */ |
46 | 45 | @Deprecated(forRemoval = true) |
47 | 46 | public LoadBalancerRestClientBuilderBeanPostProcessor(T loadBalancerInterceptor, ApplicationContext context) { |
48 | | - this.loadBalancerInterceptorProvider = new SimpleObjectProvider<>(loadBalancerInterceptor); |
49 | | - this.context = context; |
| 47 | + this(new SimpleObjectProvider<>(loadBalancerInterceptor), context); |
50 | 48 | } |
51 | 49 |
|
52 | 50 | /** |
53 | | - * Creates a {@link LoadBalancerRestClientBuilderBeanPostProcessor} instance using interceptor {@link ObjectProvider} and application context. |
54 | | - * @param loadBalancerInterceptorProvider an {@link ObjectProvider} for {@link ClientHttpRequestInterceptor} used for load-balancing |
| 51 | + * Creates a {@link LoadBalancerRestClientBuilderBeanPostProcessor} instance using |
| 52 | + * interceptor {@link ObjectProvider} and application context. |
| 53 | + * @param loadBalancerInterceptorProvider an {@link ObjectProvider} for |
| 54 | + * {@link ClientHttpRequestInterceptor} used for load-balancing |
55 | 55 | * @param context {@link ApplicationContext} |
56 | 56 | */ |
57 | 57 | public LoadBalancerRestClientBuilderBeanPostProcessor(ObjectProvider<T> loadBalancerInterceptorProvider, |
58 | 58 | ApplicationContext context) { |
59 | | - this.loadBalancerInterceptorProvider = loadBalancerInterceptorProvider; |
60 | | - this.context = context; |
| 59 | + super(loadBalancerInterceptorProvider, context); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected Object apply(Object bean, ClientHttpRequestInterceptor interceptor) { |
| 64 | + return ((RestClient.Builder) bean).requestInterceptor(interceptor); |
61 | 65 | } |
62 | 66 |
|
63 | 67 | @Override |
64 | | - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
65 | | - if (bean instanceof RestClient.Builder) { |
66 | | - if (context.findAnnotationOnBean(beanName, LoadBalanced.class) == null) { |
67 | | - return bean; |
68 | | - } |
69 | | - ClientHttpRequestInterceptor interceptor = loadBalancerInterceptorProvider.getIfAvailable(); |
70 | | - if (interceptor == null) { |
71 | | - throw new IllegalStateException(ClientHttpRequestInterceptor.class.getSimpleName() + " not available."); |
72 | | - } |
73 | | - ((RestClient.Builder) bean).requestInterceptor(interceptor); |
74 | | - } |
75 | | - return bean; |
| 68 | + protected boolean isSupported(Object bean) { |
| 69 | + return bean instanceof RestClient.Builder; |
76 | 70 | } |
77 | 71 |
|
78 | 72 | } |
0 commit comments