From d96115c1b8ddfdd36e16a62ce9d7e33949806184 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 16 Jan 2015 13:28:38 -0700 Subject: [PATCH] uses a ImportSelector that imports the NoopDiscoveryClientConfiguration if @EnableDiscoveryClient is not used. fixes gh-6 --- .../SingleImplementationImportSelector.java | 5 ++ .../NoopDiscoveryClientConfiguration.java | 5 +- .../NoopDiscoveryClientImportSelector.java | 46 +++++++++++++++++++ src/main/resources/META-INF/spring.factories | 2 +- 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientImportSelector.java diff --git a/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java b/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java index e6410712..132ea6d3 100644 --- a/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java +++ b/src/main/java/org/springframework/cloud/client/SingleImplementationImportSelector.java @@ -71,6 +71,11 @@ public abstract class SingleImplementationImportSelector implements 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.size() > 1) { String factory = factories.get(0); // there should only every be one DiscoveryClient diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java index cd7c27ea..09ff6e07 100644 --- a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientConfiguration.java @@ -22,9 +22,8 @@ import java.net.UnknownHostException; import javax.annotation.PostConstruct; import lombok.extern.apachecommons.CommonsLog; + import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; @@ -42,8 +41,6 @@ import org.springframework.core.env.Environment; */ @Configuration @EnableConfigurationProperties -@ConditionalOnMissingClass(name = "com.netflix.discovery.EurekaClientConfig") -@ConditionalOnProperty(value = "eureka.client.enabled", havingValue = "false") @CommonsLog public class NoopDiscoveryClientConfiguration implements ApplicationListener { diff --git a/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientImportSelector.java b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientImportSelector.java new file mode 100644 index 00000000..8a5190df --- /dev/null +++ b/src/main/java/org/springframework/cloud/client/discovery/NoopDiscoveryClientImportSelector.java @@ -0,0 +1,46 @@ +/* + * 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.discovery; + +import lombok.extern.apachecommons.CommonsLog; + +import org.springframework.context.annotation.DeferredImportSelector; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.type.AnnotationMetadata; + +/** + * Import Selector that only returns a NoopDiscoveryClientConfiguration if the + * @EnableDiscoveryClient annotation has NOT been used. + * @author Spencer Gibb + */ +@CommonsLog +public class NoopDiscoveryClientImportSelector implements + DeferredImportSelector { + + @Override + public String[] selectImports(AnnotationMetadata metadata) { + AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata + .getAnnotationAttributes(EnableDiscoveryClient.class.getName(), true)); + + if (attributes == null) { + // @EnableDiscoveryClient has not been used, so configure the noop client + return new String[] { NoopDiscoveryClientConfiguration.class.getName() }; + } + + return new String[]{}; + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 9ea05d30..d5290402 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,4 +1,4 @@ # Bootstrap Configuration org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.client.CommonsClientAutoConfiguration,\ -org.springframework.cloud.client.discovery.NoopDiscoveryClientConfiguration +org.springframework.cloud.client.discovery.NoopDiscoveryClientImportSelector