From b4c5aea1526cb36ecbd7bd1e0df04118ee7957f5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 3 Oct 2018 14:37:31 +0200 Subject: [PATCH] Fix detection of WebApplicationType with context class Closes gh-14589 --- .../boot/SpringApplication.java | 18 ++++++------ .../boot/SpringApplicationTests.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index c03abea60ac..f2b37604dc6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -43,6 +43,7 @@ import org.springframework.boot.Banner.Mode; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; +import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; @@ -1181,18 +1182,19 @@ public class SpringApplication { public void setApplicationContextClass( Class applicationContextClass) { this.applicationContextClass = applicationContextClass; - if (!isWebApplicationContext(applicationContextClass)) { - this.webApplicationType = WebApplicationType.NONE; - } + this.webApplicationType = deduceWebApplicationType(applicationContextClass); } - private boolean isWebApplicationContext(Class applicationContextClass) { - try { - return WebApplicationContext.class.isAssignableFrom(applicationContextClass); + private WebApplicationType deduceWebApplicationType( + Class applicationContextClass) { + if (WebApplicationContext.class.isAssignableFrom(applicationContextClass)) { + return WebApplicationType.SERVLET; } - catch (NoClassDefFoundError ex) { - return false; + if (ReactiveWebApplicationContext.class + .isAssignableFrom(applicationContextClass)) { + return WebApplicationType.REACTIVE; } + return WebApplicationType.NONE; } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 8a4ee6cb7cf..e7f742b7fc6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -58,6 +58,7 @@ import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.boot.testsupport.rule.OutputCapture; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; @@ -96,6 +97,7 @@ import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.context.ConfigurableWebEnvironment; import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.StandardServletEnvironment; import static org.assertj.core.api.Assertions.assertThat; @@ -316,6 +318,32 @@ public class SpringApplicationTests { assertThat(this.context).isInstanceOf(StaticApplicationContext.class); } + @Test + public void specificWebApplicationContextClassDetectWebApplicationType() { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application + .setApplicationContextClass(AnnotationConfigWebApplicationContext.class); + assertThat(application.getWebApplicationType()) + .isEqualTo(WebApplicationType.SERVLET); + } + + @Test + public void specificReactiveApplicationContextClassDetectReactiveApplicationType() { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setApplicationContextClass( + AnnotationConfigReactiveWebApplicationContext.class); + assertThat(application.getWebApplicationType()) + .isEqualTo(WebApplicationType.REACTIVE); + } + + @Test + public void nonWebNorReactiveApplicationContextClassDetectNoneApplicationType() { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setApplicationContextClass(StaticApplicationContext.class); + assertThat(application.getWebApplicationType()) + .isEqualTo(WebApplicationType.NONE); + } + @Test public void specificApplicationContextInitializer() { SpringApplication application = new SpringApplication(ExampleConfig.class);