Commit b4c5aea1 authored by Stephane Nicoll's avatar Stephane Nicoll

Fix detection of WebApplicationType with context class

Closes gh-14589
parent 73e6a39b
......@@ -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<? extends ConfigurableApplicationContext> 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;
}
/**
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment