Skip to content

Commit 00e122c

Browse files
committed
Merge branch '2.0.x'
2 parents a20046f + b4c5aea commit 00e122c

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
@@ -46,6 +46,7 @@
4646
import org.springframework.boot.context.properties.bind.Binder;
4747
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
4848
import org.springframework.boot.convert.ApplicationConversionService;
49+
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
4950
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
5051
import org.springframework.context.ApplicationContext;
5152
import org.springframework.context.ApplicationContextInitializer;
@@ -1195,18 +1196,19 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
11951196
public void setApplicationContextClass(
11961197
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
11971198
this.applicationContextClass = applicationContextClass;
1198-
if (!isWebApplicationContext(applicationContextClass)) {
1199-
this.webApplicationType = WebApplicationType.NONE;
1200-
}
1199+
this.webApplicationType = deduceWebApplicationType(applicationContextClass);
12011200
}
12021201

1203-
private boolean isWebApplicationContext(Class<?> applicationContextClass) {
1204-
try {
1205-
return WebApplicationContext.class.isAssignableFrom(applicationContextClass);
1202+
private WebApplicationType deduceWebApplicationType(
1203+
Class<?> applicationContextClass) {
1204+
if (WebApplicationContext.class.isAssignableFrom(applicationContextClass)) {
1205+
return WebApplicationType.SERVLET;
12061206
}
1207-
catch (NoClassDefFoundError ex) {
1208-
return false;
1207+
if (ReactiveWebApplicationContext.class
1208+
.isAssignableFrom(applicationContextClass)) {
1209+
return WebApplicationType.REACTIVE;
12091210
}
1211+
return WebApplicationType.NONE;
12101212
}
12111213

12121214
/**

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
@@ -60,6 +60,7 @@
6060
import org.springframework.boot.testsupport.rule.OutputCapture;
6161
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
6262
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
63+
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
6364
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
6465
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
6566
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
@@ -99,6 +100,7 @@
99100
import org.springframework.util.StringUtils;
100101
import org.springframework.web.context.ConfigurableWebEnvironment;
101102
import org.springframework.web.context.WebApplicationContext;
103+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
102104
import org.springframework.web.context.support.StandardServletEnvironment;
103105

104106
import static org.assertj.core.api.Assertions.assertThat;
@@ -319,6 +321,32 @@ public void specificApplicationContextClass() {
319321
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
320322
}
321323

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

0 commit comments

Comments
 (0)