Browse Source

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

Following the changes made in a7f148091e 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
pull/5092/merge
Andy Wilkinson 9 years ago
parent
commit
5810ae28d5
  1. 2
      spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 13
      spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

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

@ -339,7 +339,6 @@ public class SpringApplication { @@ -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 { @@ -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);

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

@ -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.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; @@ -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 { @@ -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<ConfigurableEnvironment> matchingPropertySource(
final Class<?> propertySourceClass, final String name) {
return new Condition<ConfigurableEnvironment>("has property source") {

Loading…
Cancel
Save