001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.web.reactive.function.client;
018
019import java.util.List;
020
021import org.springframework.beans.factory.ObjectProvider;
022import org.springframework.boot.autoconfigure.AutoConfigureAfter;
023import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
028import org.springframework.boot.web.codec.CodecCustomizer;
029import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
030import org.springframework.context.annotation.Bean;
031import org.springframework.context.annotation.Configuration;
032import org.springframework.context.annotation.Scope;
033import org.springframework.core.annotation.Order;
034import org.springframework.web.reactive.function.client.WebClient;
035
036/**
037 * {@link EnableAutoConfiguration Auto-configuration} for {@link WebClient}.
038 * <p>
039 * This will produce a
040 * {@link org.springframework.web.reactive.function.client.WebClient.Builder
041 * WebClient.Builder} bean with the {@code prototype} scope, meaning each injection point
042 * will receive a newly cloned instance of the builder.
043 *
044 * @author Brian Clozel
045 * @since 2.0.0
046 */
047@Configuration
048@ConditionalOnClass(WebClient.class)
049@AutoConfigureAfter({ CodecsAutoConfiguration.class,
050                ClientHttpConnectorAutoConfiguration.class })
051public class WebClientAutoConfiguration {
052
053        private final WebClient.Builder webClientBuilder;
054
055        public WebClientAutoConfiguration(
056                        ObjectProvider<WebClientCustomizer> customizerProvider) {
057                this.webClientBuilder = WebClient.builder();
058                customizerProvider.orderedStream()
059                                .forEach((customizer) -> customizer.customize(this.webClientBuilder));
060        }
061
062        @Bean
063        @Scope("prototype")
064        @ConditionalOnMissingBean
065        public WebClient.Builder webClientBuilder() {
066                return this.webClientBuilder.clone();
067        }
068
069        @Configuration
070        @ConditionalOnBean(CodecCustomizer.class)
071        protected static class WebClientCodecsConfiguration {
072
073                @Bean
074                @ConditionalOnMissingBean
075                @Order(0)
076                public WebClientCodecCustomizer exchangeStrategiesCustomizer(
077                                List<CodecCustomizer> codecCustomizers) {
078                        return new WebClientCodecCustomizer(codecCustomizers);
079                }
080
081        }
082
083}