/*
 * Copyright 2013-2015 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
 *
 *      http://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 java.util.ArrayList;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

/**
 * Auto configuration for Ribbon (client side load balancing).
 *
 * @author Spencer Gibb
 * @author Dave Syer
 */
@Configuration
@ConditionalOnClass(RestTemplate.class)
@ConditionalOnBean(LoadBalancerClient.class)
public class LoadBalancerAutoConfiguration {

	@Bean
    @LoadBalanced
	public RestTemplate loadBalancedRestTemplate(RestTemplateCustomizer customizer) {
		RestTemplate restTemplate = new RestTemplate();
        customizer.customize(restTemplate);
		return restTemplate;
	}

    @Bean
    @ConditionalOnMissingBean
    public RestTemplateCustomizer restTemplateCustomizer(final LoadBalancerInterceptor loadBalancerInterceptor) {
        return new RestTemplateCustomizer() {
            @Override
            public void customize(RestTemplate restTemplate) {
                List<ClientHttpRequestInterceptor> list = new ArrayList<>();
                list.add(loadBalancerInterceptor);
                restTemplate.setInterceptors(list);
            }
        };
    }

	@Bean
	public LoadBalancerInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient) {
		return new LoadBalancerInterceptor(loadBalancerClient);
	}

}

@@ -1463,20 +1463,17 @@
 %0A%09@Bean%0A
-    
+%09
 @LoadBal
@@ -1524,16 +1524,25 @@
 emplate(
+%0A%09%09%09List%3C
 RestTemp
@@ -1555,16 +1555,17 @@
 stomizer
+%3E
  customi
@@ -1567,16 +1567,17 @@
 stomizer
+s
 ) %7B%0A%09%09Re
@@ -1622,24 +1622,77 @@
 late();%0A
-        
+%09%09for (RestTemplateCustomizer customizer : customizers) %7B%0A%09%09%09
 customiz
@@ -1719,16 +1719,20 @@
 plate);%0A
+%09%09%7D%0A
 %09%09return
@@ -1754,22 +1754,16 @@
 %09%7D%0A%0A
-    
+%09
 @Bean%0A
-    
+%09
 @Con
@@ -1784,20 +1784,17 @@
 ingBean%0A
-    
+%09
 public R
@@ -1838,16 +1838,20 @@
 tomizer(
+%0A%09%09%09
 final Lo
@@ -1899,24 +1899,18 @@
 ptor) %7B%0A
-        
+%09%09
 return n
@@ -1943,42 +1943,24 @@
 ) %7B%0A
-            @Override%0A            
+%09%09%09@Override%0A%09%09%09
 publ
@@ -2006,32 +2006,20 @@
 late) %7B%0A
-                
+%09%09%09%09
 List%3CCli
@@ -2071,32 +2071,20 @@
 st%3C%3E();%0A
-                
+%09%09%09%09
 list.add
@@ -2114,24 +2114,12 @@
 r);%0A
-                
+%09%09%09%09
 rest
@@ -2154,37 +2154,19 @@
 t);%0A
-            %7D%0A        %7D;%0A    
+%09%09%09%7D%0A%09%09%7D;%0A%09
 %7D%0A%0A%09
@@ -2221,16 +2221,20 @@
 rceptor(
+%0A%09%09%09
 LoadBala

Allow multiple rest template customizers