Skip to content

Commit 47c77cb

Browse files
author
Dave Syer
committed
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 spring-projectsgh-80
1 parent 457d237 commit 47c77cb

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@
2525
* @author Spencer Gibb
2626
*/
2727
@Order(Ordered.LOWEST_PRECEDENCE - 100)
28-
public class EnableDiscoveryClientImportSelector extends
29-
SpringFactoryImportSelector<EnableDiscoveryClient> {
28+
public class EnableDiscoveryClientImportSelector
29+
extends SpringFactoryImportSelector<EnableDiscoveryClient> {
3030

3131
@Override
3232
protected boolean isEnabled() {
3333
return new RelaxedPropertyResolver(getEnvironment()).getProperty(
3434
"spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
3535
}
3636

37+
@Override
38+
protected boolean hasDefaultFactory() {
39+
return true;
40+
}
41+
3742
}

spring-cloud-commons/src/main/java/org/springframework/cloud/util/SpringFactoryImportSelector.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.util.LinkedHashSet;
2121
import java.util.List;
2222

23-
import lombok.extern.apachecommons.CommonsLog;
24-
2523
import org.springframework.beans.factory.BeanClassLoaderAware;
2624
import org.springframework.context.EnvironmentAware;
2725
import org.springframework.context.annotation.DeferredImportSelector;
@@ -32,16 +30,18 @@
3230
import org.springframework.core.type.AnnotationMetadata;
3331
import org.springframework.util.Assert;
3432

33+
import lombok.extern.apachecommons.CommonsLog;
34+
3535
/**
36-
* Selects configurations to load defined by the generic type T. Loads
37-
* implementations using {@link SpringFactoriesLoader}.
36+
* Selects configurations to load defined by the generic type T. Loads implementations
37+
* using {@link SpringFactoriesLoader}.
3838
*
3939
* @author Spencer Gibb
4040
* @author Dave Syer
4141
*/
4242
@CommonsLog
43-
public abstract class SpringFactoryImportSelector<T> implements
44-
DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
43+
public abstract class SpringFactoryImportSelector<T>
44+
implements DeferredImportSelector, BeanClassLoaderAware, EnvironmentAware {
4545

4646
private ClassLoader beanClassLoader;
4747

@@ -51,40 +51,44 @@ public abstract class SpringFactoryImportSelector<T> implements
5151

5252
@SuppressWarnings("unchecked")
5353
protected SpringFactoryImportSelector() {
54-
this.annotationClass = (Class<T>) GenericTypeResolver.resolveTypeArgument(
55-
this.getClass(), SpringFactoryImportSelector.class);
54+
this.annotationClass = (Class<T>) GenericTypeResolver
55+
.resolveTypeArgument(this.getClass(), SpringFactoryImportSelector.class);
5656
}
5757

5858
@Override
5959
public String[] selectImports(AnnotationMetadata metadata) {
6060
if (!isEnabled()) {
6161
return new String[0];
6262
}
63-
AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
64-
.getAnnotationAttributes(this.annotationClass.getName(), true));
63+
AnnotationAttributes attributes = AnnotationAttributes.fromMap(
64+
metadata.getAnnotationAttributes(this.annotationClass.getName(), true));
6565

6666
Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is "
6767
+ metadata.getClassName() + " annotated with @" + getSimpleName() + "?");
6868

6969
// Find all possible auto configuration classes, filtering duplicates
70-
List<String> factories = new ArrayList<>(new LinkedHashSet<>(
71-
SpringFactoriesLoader.loadFactoryNames(this.annotationClass,
72-
this.beanClassLoader)));
70+
List<String> factories = new ArrayList<>(new LinkedHashSet<>(SpringFactoriesLoader
71+
.loadFactoryNames(this.annotationClass, this.beanClassLoader)));
7372

74-
if (factories.isEmpty()) {
75-
throw new IllegalStateException("Annotation @" + getSimpleName() +
76-
" found, but there are no implementations. Did you forget to include a starter?");
73+
if (factories.isEmpty() && !hasDefaultFactory()) {
74+
throw new IllegalStateException("Annotation @" + getSimpleName()
75+
+ " found, but there are no implementations. Did you forget to include a starter?");
7776
}
7877

7978
if (factories.size() > 1) {
80-
// there should only ever be one DiscoveryClient, but there might be more than one factory
79+
// there should only ever be one DiscoveryClient, but there might be more than
80+
// one factory
8181
log.warn("More than one implementation " + "of @" + getSimpleName()
8282
+ " (now relying on @Conditionals to pick one): " + factories);
8383
}
8484

8585
return factories.toArray(new String[factories.size()]);
8686
}
8787

88+
protected boolean hasDefaultFactory() {
89+
return false;
90+
}
91+
8892
protected abstract boolean isEnabled();
8993

9094
protected String getSimpleName() {

0 commit comments

Comments
 (0)