From 5810ae28d515ef40e606a6c0ab233cf2a1dbcdb8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 11 May 2017 15:57:53 +0100 Subject: [PATCH] Ensure that WebApplicationType.NONE results in a non-web environment Following the changes made in a7f148091eb9 the environment was being bound to the SpringApplication instance after it had, if necessary being converted to a standard, i.e non-web environment. This meant that if a property in the environment set the web application type to NONE it would have no effect on the type of environment used by the application. This commit reorders the binding of the environment to the Spring Application instance so that it happens before the environment is potentially converted. Closes gh-9161 --- .../org/springframework/boot/SpringApplication.java | 2 +- .../boot/SpringApplicationTests.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index f1709cb5b28..6328c5f6067 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -339,7 +339,6 @@ public class SpringApplication { ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); configureIgnoreBeanInfo(environment); - bindToSpringApplication(environment); Banner printedBanner = printBanner(environment); context = createApplicationContext(); exceptionReporters = getSpringFactoriesInstances( @@ -370,6 +369,7 @@ public class SpringApplication { ConfigurableEnvironment environment = getOrCreateEnvironment(); configureEnvironment(environment, applicationArguments.getSourceArgs()); listeners.environmentPrepared(environment); + bindToSpringApplication(environment); if (isWebEnvironment(environment) && this.webApplicationType == WebApplicationType.NONE) { environment = convertToStandardEnvironment(environment); diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 2b827d9dd74..0fc86064ead 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -51,6 +51,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.context.ApplicationContext; @@ -84,6 +85,8 @@ import org.springframework.http.server.reactive.HttpHandler; import org.springframework.test.context.support.TestPropertySourceUtils; 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.StandardServletEnvironment; import static org.assertj.core.api.Assertions.assertThat; @@ -890,6 +893,16 @@ public class SpringApplicationTests { assertThat(occurrences).as("Expected single stacktrace").isEqualTo(1); } + @Test + public void nonWebApplicationConfiguredViaAPropertyHasTheCorrectTypeOfContextAndEnvironment() { + ConfigurableApplicationContext context = new SpringApplication( + ExampleConfig.class).run("--spring.main.web-application-type=NONE"); + assertThat(context).isNotInstanceOfAny(WebApplicationContext.class, + ReactiveWebApplicationContext.class); + assertThat(context.getEnvironment()) + .isNotInstanceOfAny(ConfigurableWebEnvironment.class); + } + private Condition matchingPropertySource( final Class propertySourceClass, final String name) { return new Condition("has property source") {