Fix discovery client if enabled but no impl available
If user has @EnableDiscoveryClient but no implementation (e.g. if he just has @EnableZuulProxy) the autoconfig will now kick in and provide an instance of NoopDiscoveryClient. Fixes gh-80
This commit is contained in:
@@ -25,8 +25,8 @@ import org.springframework.core.annotation.Order;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Order(Ordered.LOWEST_PRECEDENCE - 100)
|
||||
public class EnableDiscoveryClientImportSelector extends
|
||||
SpringFactoryImportSelector<EnableDiscoveryClient> {
|
||||
public class EnableDiscoveryClientImportSelector
|
||||
extends SpringFactoryImportSelector<EnableDiscoveryClient> {
|
||||
|
||||
@Override
|
||||
protected boolean isEnabled() {
|
||||
@@ -34,4 +34,9 @@ public class EnableDiscoveryClientImportSelector extends
|
||||
"spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean hasDefaultFactory() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.DeferredImportSelector;
|
||||
@@ -32,16 +30,18 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
/**
|
||||
* Selects configurations to load defined by the generic type T. Loads
|
||||
* implementations using {@link SpringFactoriesLoader}.
|
||||
* Selects configurations to load defined by the generic type T. Loads implementations
|
||||
* using {@link SpringFactoriesLoader}.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@CommonsLog
|
||||
public abstract class SpringFactoryImportSelector<T> implements
|
||||
DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
|
||||
public abstract class SpringFactoryImportSelector<T>
|
||||
implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
@@ -51,8 +51,8 @@ public abstract class SpringFactoryImportSelector<T> implements
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected SpringFactoryImportSelector() {
|
||||
this.annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(
|
||||
this.getClass(), SpringFactoryImportSelector.class);
|
||||
this.annotationClass = (Class<T>) GenericTypeResolver
|
||||
.resolveTypeArgument(this.getClass(), SpringFactoryImportSelector.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,24 +60,24 @@ public abstract class SpringFactoryImportSelector<T> implements
|
||||
if (!isEnabled()) {
|
||||
return new String[0];
|
||||
}
|
||||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
|
||||
.getAnnotationAttributes(this.annotationClass.getName(), true));
|
||||
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
|
||||
metadata.getAnnotationAttributes(this.annotationClass.getName(), true));
|
||||
|
||||
Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is "
|
||||
+ metadata.getClassName() + " annotated with @" + getSimpleName() + "?");
|
||||
|
||||
// Find all possible auto configuration classes, filtering duplicates
|
||||
List<String> factories = new ArrayList<>(new LinkedHashSet<>(
|
||||
SpringFactoriesLoader.loadFactoryNames(this.annotationClass,
|
||||
this.beanClassLoader)));
|
||||
List<String> factories = new ArrayList<>(new LinkedHashSet<>(SpringFactoriesLoader
|
||||
.loadFactoryNames(this.annotationClass, this.beanClassLoader)));
|
||||
|
||||
if (factories.isEmpty()) {
|
||||
throw new IllegalStateException("Annotation @" + getSimpleName() +
|
||||
" found, but there are no implementations. Did you forget to include a starter?");
|
||||
if (factories.isEmpty() && !hasDefaultFactory()) {
|
||||
throw new IllegalStateException("Annotation @" + getSimpleName()
|
||||
+ " found, but there are no implementations. Did you forget to include a starter?");
|
||||
}
|
||||
|
||||
if (factories.size() > 1) {
|
||||
// there should only ever be one DiscoveryClient, but there might be more than one factory
|
||||
// there should only ever be one DiscoveryClient, but there might be more than
|
||||
// one factory
|
||||
log.warn("More than one implementation " + "of @" + getSimpleName()
|
||||
+ " (now relying on @Conditionals to pick one): " + factories);
|
||||
}
|
||||
@@ -85,6 +85,10 @@ public abstract class SpringFactoryImportSelector<T> implements
|
||||
return factories.toArray(new String[factories.size()]);
|
||||
}
|
||||
|
||||
protected boolean hasDefaultFactory() {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected abstract boolean isEnabled();
|
||||
|
||||
protected String getSimpleName() {
|
||||
|
||||
Reference in New Issue
Block a user