Be defensive about web classes being available

This commit is contained in:
Dave Syer
2017-05-16 14:48:19 +01:00
parent 206ba7bcc1
commit 01cca0275a

View File

@@ -16,6 +16,7 @@ import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;
/**
* Spring Boot Auto-Configuration for Simple Properties based Discovery Client
@@ -59,11 +60,15 @@ public class SimpleDiscoveryClientAutoConfiguration {
}
private int findPort() {
if (this.context instanceof EmbeddedWebApplicationContext) {
EmbeddedServletContainer container = ((EmbeddedWebApplicationContext) this.context)
.getEmbeddedServletContainer();
if (container != null) {
return container.getPort();
if (ClassUtils.isPresent(
"org.springframework.boot.context.embedded.EmbeddedWebApplicationContext",
null)) {
if (this.context instanceof EmbeddedWebApplicationContext) {
EmbeddedServletContainer container = ((EmbeddedWebApplicationContext) this.context)
.getEmbeddedServletContainer();
if (container != null) {
return container.getPort();
}
}
}
if (this.server != null && this.server.getPort() != null