Commit 5810ae28 authored by Andy Wilkinson's avatar Andy Wilkinson

Ensure that WebApplicationType.NONE results in a non-web environment

Following the changes made in a7f14809 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
parent 1bf7558a
...@@ -339,7 +339,6 @@ public class SpringApplication { ...@@ -339,7 +339,6 @@ public class SpringApplication {
ConfigurableEnvironment environment = prepareEnvironment(listeners, ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments); applicationArguments);
configureIgnoreBeanInfo(environment); configureIgnoreBeanInfo(environment);
bindToSpringApplication(environment);
Banner printedBanner = printBanner(environment); Banner printedBanner = printBanner(environment);
context = createApplicationContext(); context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances( exceptionReporters = getSpringFactoriesInstances(
...@@ -370,6 +369,7 @@ public class SpringApplication { ...@@ -370,6 +369,7 @@ public class SpringApplication {
ConfigurableEnvironment environment = getOrCreateEnvironment(); ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs()); configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment); listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (isWebEnvironment(environment) if (isWebEnvironment(environment)
&& this.webApplicationType == WebApplicationType.NONE) { && this.webApplicationType == WebApplicationType.NONE) {
environment = convertToStandardEnvironment(environment); environment = convertToStandardEnvironment(environment);
......
...@@ -51,6 +51,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent; ...@@ -51,6 +51,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.boot.testutil.InternalOutputCapture;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; 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.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -84,6 +85,8 @@ import org.springframework.http.server.reactive.HttpHandler; ...@@ -84,6 +85,8 @@ import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; 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 org.springframework.web.context.support.StandardServletEnvironment;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -890,6 +893,16 @@ public class SpringApplicationTests { ...@@ -890,6 +893,16 @@ public class SpringApplicationTests {
assertThat(occurrences).as("Expected single stacktrace").isEqualTo(1); 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<ConfigurableEnvironment> matchingPropertySource( private Condition<ConfigurableEnvironment> matchingPropertySource(
final Class<?> propertySourceClass, final String name) { final Class<?> propertySourceClass, final String name) {
return new Condition<ConfigurableEnvironment>("has property source") { return new Condition<ConfigurableEnvironment>("has property source") {
......
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