Skip to content

Commit b4c5aea

Browse files
committed
Fix detection of WebApplicationType with context class
Closes gh-14589
1 parent 73e6a39 commit b4c5aea

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.boot.context.properties.bind.Bindable;
4444
import org.springframework.boot.context.properties.bind.Binder;
4545
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
46+
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
4647
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
4748
import org.springframework.context.ApplicationContext;
4849
import org.springframework.context.ApplicationContextInitializer;
@@ -1181,18 +1182,19 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
11811182
public void setApplicationContextClass(
11821183
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
11831184
this.applicationContextClass = applicationContextClass;
1184-
if (!isWebApplicationContext(applicationContextClass)) {
1185-
this.webApplicationType = WebApplicationType.NONE;
1186-
}
1185+
this.webApplicationType = deduceWebApplicationType(applicationContextClass);
11871186
}
11881187

1189-
private boolean isWebApplicationContext(Class<?> applicationContextClass) {
1190-
try {
1191-
return WebApplicationContext.class.isAssignableFrom(applicationContextClass);
1188+
private WebApplicationType deduceWebApplicationType(
1189+
Class<?> applicationContextClass) {
1190+
if (WebApplicationContext.class.isAssignableFrom(applicationContextClass)) {
1191+
return WebApplicationType.SERVLET;
11921192
}
1193-
catch (NoClassDefFoundError ex) {
1194-
return false;
1193+
if (ReactiveWebApplicationContext.class
1194+
.isAssignableFrom(applicationContextClass)) {
1195+
return WebApplicationType.REACTIVE;
11951196
}
1197+
return WebApplicationType.NONE;
11961198
}
11971199

11981200
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.boot.testsupport.rule.OutputCapture;
5959
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
6060
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
61+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
6162
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
6263
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
6364
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
@@ -96,6 +97,7 @@
9697
import org.springframework.util.StringUtils;
9798
import org.springframework.web.context.ConfigurableWebEnvironment;
9899
import org.springframework.web.context.WebApplicationContext;
100+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
99101
import org.springframework.web.context.support.StandardServletEnvironment;
100102

101103
import static org.assertj.core.api.Assertions.assertThat;
@@ -316,6 +318,32 @@ public void specificApplicationContextClass() {
316318
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
317319
}
318320

321+
@Test
322+
public void specificWebApplicationContextClassDetectWebApplicationType() {
323+
SpringApplication application = new SpringApplication(ExampleConfig.class);
324+
application
325+
.setApplicationContextClass(AnnotationConfigWebApplicationContext.class);
326+
assertThat(application.getWebApplicationType())
327+
.isEqualTo(WebApplicationType.SERVLET);
328+
}
329+
330+
@Test
331+
public void specificReactiveApplicationContextClassDetectReactiveApplicationType() {
332+
SpringApplication application = new SpringApplication(ExampleConfig.class);
333+
application.setApplicationContextClass(
334+
AnnotationConfigReactiveWebApplicationContext.class);
335+
assertThat(application.getWebApplicationType())
336+
.isEqualTo(WebApplicationType.REACTIVE);
337+
}
338+
339+
@Test
340+
public void nonWebNorReactiveApplicationContextClassDetectNoneApplicationType() {
341+
SpringApplication application = new SpringApplication(ExampleConfig.class);
342+
application.setApplicationContextClass(StaticApplicationContext.class);
343+
assertThat(application.getWebApplicationType())
344+
.isEqualTo(WebApplicationType.NONE);
345+
}
346+
319347
@Test
320348
public void specificApplicationContextInitializer() {
321349
SpringApplication application = new SpringApplication(ExampleConfig.class);

0 commit comments

Comments
 (0)