Merge branch '2.0.x'

This commit is contained in:
Stephane Nicoll 2018-10-03 14:39:15 +02:00
commit 00e122c7ec
2 changed files with 38 additions and 8 deletions

View File

@ -46,6 +46,7 @@ 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.convert.ApplicationConversionService;
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;
@ -1195,18 +1196,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;
}
/**

View File

@ -60,6 +60,7 @@ import org.springframework.boot.convert.ApplicationConversionService;
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;
@ -99,6 +100,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;
@ -319,6 +321,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);