Commit cf615282 by Dave Syer

Move @Autowired into @Configuration

I prefer to keep the actual components free of autowiring, so they are easier to navigate in an IDE (i.e. you can see how they are constructed)
parent 66b4d4c6
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
...@@ -13,51 +17,56 @@ import org.springframework.web.client.RestTemplate; ...@@ -13,51 +17,56 @@ import org.springframework.web.client.RestTemplate;
import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.BaseLoadBalancer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Configuration @Configuration
@AutoConfigureAfter(EurekaClientAutoConfiguration.class) @AutoConfigureAfter(EurekaClientAutoConfiguration.class)
public class RibbonAutoConfiguration { public class RibbonAutoConfiguration {
@Autowired(required=false) @Autowired(required = false)
private List<BaseLoadBalancer> balancers = Collections.emptyList(); private List<BaseLoadBalancer> balancers = Collections.emptyList();
@Autowired(required=false) @Autowired
private EurekaRibbonClientPreprocessor clientPreprocessor; private EurekaRibbonClientPreprocessor clientPreprocessor;
// TODO: need to find a default for this?
@Autowired
private SpringClientFactory springClientFactory;
@Bean
@ConditionalOnMissingBean(RestTemplate.class)
public RestTemplate restTemplate(RibbonInterceptor ribbonInterceptor) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> list = new ArrayList<>();
list.add(ribbonInterceptor);
restTemplate.setInterceptors(list);
return restTemplate;
}
@Bean
@ConditionalOnMissingBean(LoadBalancerClient.class)
public LoadBalancerClient loadBalancerClient() {
return new RibbonLoadBalancerClient(clientPreprocessor, springClientFactory, balancers);
}
@Bean
public RibbonInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient) {
return new RibbonInterceptor(loadBalancerClient);
}
@Configuration
protected static class DefaultRibbonClientPreprocessor {
@Bean
public RibbonClientPreprocessor ribbonClientPreprocessor() {
return new RibbonClientPreprocessor() {
@Override
public void preprocess(String serviceId) {
// no-op
}
};
}
@Bean }
@ConditionalOnMissingBean(RestTemplate.class)
public RestTemplate restTemplate(RibbonInterceptor ribbonInterceptor) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> list = new ArrayList<>();
list.add(ribbonInterceptor);
restTemplate.setInterceptors(list);
return restTemplate;
}
@Bean
@ConditionalOnMissingBean(LoadBalancerClient.class)
public LoadBalancerClient loadBalancerClient() {
return new RibbonLoadBalancerClient(balancers);
}
@Bean
public RibbonInterceptor ribbonInterceptor(LoadBalancerClient loadBalancerClient) {
return new RibbonInterceptor(loadBalancerClient);
}
@Bean
public RibbonClientPreprocessor ribbonClientPreprocessor() {
return new RibbonClientPreprocessor() {
@Override
public void preprocess(String serviceId) {
//no-op
}
};
}
} }
...@@ -4,15 +4,14 @@ import java.util.HashMap; ...@@ -4,15 +4,14 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.netflix.loadbalancer.AbstractLoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import com.netflix.loadbalancer.AbstractLoadBalancer;
import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -20,15 +19,13 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; ...@@ -20,15 +19,13 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
*/ */
public class RibbonLoadBalancerClient implements LoadBalancerClient { public class RibbonLoadBalancerClient implements LoadBalancerClient {
@Autowired
private RibbonClientPreprocessor ribbonClientPreprocessor; private RibbonClientPreprocessor ribbonClientPreprocessor;
@Autowired
private SpringClientFactory clientFactory; private SpringClientFactory clientFactory;
private Map<String, ILoadBalancer> balancers = new HashMap<String, ILoadBalancer>(); private Map<String, ILoadBalancer> balancers = new HashMap<String, ILoadBalancer>();
public RibbonLoadBalancerClient(List<BaseLoadBalancer> balancers) { public RibbonLoadBalancerClient(RibbonClientPreprocessor ribbonClientPreprocessor, SpringClientFactory clientFactory, List<BaseLoadBalancer> balancers) {
for (BaseLoadBalancer balancer : balancers) { for (BaseLoadBalancer balancer : balancers) {
this.balancers.put(balancer.getName(), balancer); this.balancers.put(balancer.getName(), balancer);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment