Commit 9aa979cd by Dave Syer

@FeignClientScan -> @EnableFeignClients

By analogy with @EnableJpaRepositories, so it's obvious that the same thing will happen (interfaces are turned into concrete @Beans).
parent fe08762f
...@@ -37,8 +37,8 @@ import org.springframework.context.annotation.Import; ...@@ -37,8 +37,8 @@ import org.springframework.context.annotation.Import;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Documented @Documented
@Import(FeignClientScanRegistrar.class) @Import({ FeignClientsConfiguration.class, FeignClientsRegistrar.class })
public @interface FeignClientScan { public @interface EnableFeignClients {
/** /**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package org.springframework.cloud.netflix.feign; package org.springframework.cloud.netflix.feign;
import feign.slf4j.Slf4jLogger;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration; import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
...@@ -28,9 +27,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -28,9 +27,7 @@ import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
import feign.Client; import feign.Client;
import feign.Contract;
import feign.Feign; import feign.Feign;
import feign.Logger;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -39,28 +36,9 @@ import feign.Logger; ...@@ -39,28 +36,9 @@ import feign.Logger;
@Configuration @Configuration
@ConditionalOnClass(Feign.class) @ConditionalOnClass(Feign.class)
@AutoConfigureAfter(ArchaiusAutoConfiguration.class) @AutoConfigureAfter(ArchaiusAutoConfiguration.class)
@EnableFeignClients
public class FeignAutoConfiguration { public class FeignAutoConfiguration {
@Bean
public SpringDecoder feignDecoder() {
return new SpringDecoder();
}
@Bean
public SpringEncoder feignEncoder() {
return new SpringEncoder();
}
@Bean
public Logger feignLogger() {
return new Slf4jLogger();
}
@Bean
public Contract feignContract() {
return new SpringMvcContract();
}
@ConditionalOnClass(ILoadBalancer.class) @ConditionalOnClass(ILoadBalancer.class)
@Configuration @Configuration
protected static class RibbonClientConfiguration { protected static class RibbonClientConfiguration {
......
...@@ -24,7 +24,10 @@ import java.lang.annotation.Target; ...@@ -24,7 +24,10 @@ import java.lang.annotation.Target;
/** /**
* Annotation for interfaces declaring that a REST client with that interface should be * Annotation for interfaces declaring that a REST client with that interface should be
* created (e.g. for autowiring into another component). * created (e.g. for autowiring into another component). If ribbon is available it will be
* used to load balance the backend requests, and the load balancer can be configured
* using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
*
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
...@@ -33,14 +36,15 @@ import java.lang.annotation.Target; ...@@ -33,14 +36,15 @@ import java.lang.annotation.Target;
public @interface FeignClient { public @interface FeignClient {
/** /**
* @return serviceId if loadbalance is true, url otherwise There is no need to prefix * The serviceId if loadbalance is true, or an absolute URL otherwise There is no need
* serviceId with http://. * to prefix serviceId with http://.
*/ */
String value(); String value();
/** /**
* @return true if calls should be load balanced (assuming a load balancer is * Set to true if calls should be load balanced (assuming a load balancer is
* available). * available). If no load balancer is available this flag is ignored (and hence the
* {@link #value() value} should be an absolute URL).
*/ */
boolean loadbalance() default true; boolean loadbalance() default true;
......
...@@ -106,10 +106,6 @@ class FeignClientFactoryBean implements FactoryBean<Object> { ...@@ -106,10 +106,6 @@ class FeignClientFactoryBean implements FactoryBean<Object> {
return builder; return builder;
} }
protected <T> T loadBalance(Class<T> type, String schemeName) {
return loadBalance(feign(), type, schemeName);
}
protected <T> T loadBalance(Feign.Builder builder, Class<T> type, String schemeName) { protected <T> T loadBalance(Feign.Builder builder, Class<T> type, String schemeName) {
builder.logger(new Slf4jLogger(type)); // TODO: how to have choice here? builder.logger(new Slf4jLogger(type)); // TODO: how to have choice here?
if (this.ribbonClient != null) { if (this.ribbonClient != null) {
...@@ -126,7 +122,7 @@ class FeignClientFactoryBean implements FactoryBean<Object> { ...@@ -126,7 +122,7 @@ class FeignClientFactoryBean implements FactoryBean<Object> {
this.schemeName = "http://" + this.schemeName; this.schemeName = "http://" + this.schemeName;
} }
if (this.loadbalance) { if (this.loadbalance) {
return loadBalance(this.type, this.schemeName); return loadBalance(feign(), this.type, this.schemeName);
} }
return feign().target(this.type, this.schemeName); return feign().target(this.type, this.schemeName);
} }
......
/*
* 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.netflix.feign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
import feign.Logger;
import feign.slf4j.Slf4jLogger;
/**
* @author Dave Syer
*/
@Configuration
public class FeignClientsConfiguration {
@Bean
public SpringDecoder feignDecoder() {
return new SpringDecoder();
}
@Bean
public SpringEncoder feignEncoder() {
return new SpringEncoder();
}
@Bean
public Logger feignLogger() {
return new Slf4jLogger();
}
@Bean
public Contract feignContract() {
return new SpringMvcContract();
}
}
...@@ -41,7 +41,7 @@ import org.springframework.util.StringUtils; ...@@ -41,7 +41,7 @@ import org.springframework.util.StringUtils;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar, public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar,
ResourceLoaderAware, BeanClassLoaderAware { ResourceLoaderAware, BeanClassLoaderAware {
// patterned after Spring Integration IntegrationComponentScanRegistrar // patterned after Spring Integration IntegrationComponentScanRegistrar
...@@ -50,7 +50,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -50,7 +50,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
private ClassLoader classLoader; private ClassLoader classLoader;
public FeignClientScanRegistrar() { public FeignClientsRegistrar() {
} }
@Override @Override
...@@ -91,7 +91,8 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -91,7 +91,8 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
} }
} }
public BeanDefinitionHolder createBeanDefinition(AnnotationMetadata annotationMetadata) { private BeanDefinitionHolder createBeanDefinition(
AnnotationMetadata annotationMetadata) {
Map<String, Object> attributes = annotationMetadata Map<String, Object> attributes = annotationMetadata
.getAnnotationAttributes(FeignClient.class.getCanonicalName()); .getAnnotationAttributes(FeignClient.class.getCanonicalName());
...@@ -121,7 +122,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -121,7 +122,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
try { try {
Class<?> target = ClassUtils.forName(beanDefinition Class<?> target = ClassUtils.forName(beanDefinition
.getMetadata().getClassName(), .getMetadata().getClassName(),
FeignClientScanRegistrar.this.classLoader); FeignClientsRegistrar.this.classLoader);
return !target.isAnnotation(); return !target.isAnnotation();
} }
catch (Exception ex) { catch (Exception ex) {
...@@ -140,7 +141,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar, ...@@ -140,7 +141,7 @@ public class FeignClientScanRegistrar implements ImportBeanDefinitionRegistrar,
protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata) { protected Set<String> getBasePackages(AnnotationMetadata importingClassMetadata) {
Map<String, Object> attributes = importingClassMetadata Map<String, Object> attributes = importingClassMetadata
.getAnnotationAttributes(FeignClientScan.class.getCanonicalName()); .getAnnotationAttributes(EnableFeignClients.class.getCanonicalName());
Set<String> basePackages = new HashSet<>(); Set<String> basePackages = new HashSet<>();
for (String pkg : (String[]) attributes.get("value")) { for (String pkg : (String[]) attributes.get("value")) {
......
...@@ -91,7 +91,7 @@ public class FeignClientTests { ...@@ -91,7 +91,7 @@ public class FeignClientTests {
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@RestController @RestController
@FeignClientScan @EnableFeignClients
@RibbonClient(name = "localapp", configuration = LocalRibbonClientConfiguration.class) @RibbonClient(name = "localapp", configuration = LocalRibbonClientConfiguration.class)
protected static class Application { protected static class Application {
......
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