Skip to content

Commit 57ebdab

Browse files
committed
Delay property source initialization till LoggingSystem is initialized
Previously, the initialization of StandardServletEnvironment's property sources in SpringBootServletInitializer led to debug logging calls being made before the LoggingSystem had been initialized. As a result, the system's default configuration was used and, in the case of Logback at least, the debug logging was output to System.out in a war deployment. This commit updates SpringBootServletInitializer to delay the initialization of StandardServletEnvironment's property sources until after the LoggingSystem has been initialized, but still in time for active profiles to be configured via servlet context parameters (see gh-9972). Closes gh-13736
1 parent 73a08dd commit 57ebdab

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

spring-boot/src/main/java/org/springframework/boot/web/support/SpringBootServletInitializer.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@
2929
import org.springframework.boot.builder.ParentContextApplicationContextInitializer;
3030
import org.springframework.boot.builder.SpringApplicationBuilder;
3131
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
32+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
3233
import org.springframework.boot.web.servlet.ServletContextInitializer;
3334
import org.springframework.context.ApplicationContext;
35+
import org.springframework.context.ApplicationListener;
3436
import org.springframework.context.annotation.Configuration;
37+
import org.springframework.core.Ordered;
3538
import org.springframework.core.annotation.AnnotationUtils;
39+
import org.springframework.core.env.ConfigurableEnvironment;
3640
import org.springframework.util.Assert;
3741
import org.springframework.web.WebApplicationInitializer;
42+
import org.springframework.web.context.ConfigurableWebEnvironment;
3843
import org.springframework.web.context.ContextLoaderListener;
3944
import org.springframework.web.context.WebApplicationContext;
40-
import org.springframework.web.context.support.StandardServletEnvironment;
4145

4246
/**
4347
* An opinionated {@link WebApplicationInitializer} to run a {@link SpringApplication}
@@ -104,9 +108,6 @@ public void contextInitialized(ServletContextEvent event) {
104108
protected WebApplicationContext createRootApplicationContext(
105109
ServletContext servletContext) {
106110
SpringApplicationBuilder builder = createSpringApplicationBuilder();
107-
StandardServletEnvironment environment = new StandardServletEnvironment();
108-
environment.initPropertySources(servletContext, null);
109-
builder.environment(environment);
110111
builder.main(getClass());
111112
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
112113
if (parent != null) {
@@ -119,6 +120,7 @@ protected WebApplicationContext createRootApplicationContext(
119120
new ServletContextApplicationContextInitializer(servletContext));
120121
builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
121122
builder = configure(builder);
123+
builder.listeners(new WebEnvironmentPropertySourceInitializer(servletContext));
122124
SpringApplication application = builder.build();
123125
if (application.getSources().isEmpty() && AnnotationUtils
124126
.findAnnotation(getClass(), Configuration.class) != null) {
@@ -177,4 +179,30 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
177179
return builder;
178180
}
179181

182+
private static final class WebEnvironmentPropertySourceInitializer
183+
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
184+
185+
private final ServletContext servletContext;
186+
187+
private WebEnvironmentPropertySourceInitializer(ServletContext servletContext) {
188+
this.servletContext = servletContext;
189+
}
190+
191+
@Override
192+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
193+
ConfigurableEnvironment environment = event.getEnvironment();
194+
if (environment instanceof ConfigurableWebEnvironment) {
195+
((ConfigurableWebEnvironment) environment)
196+
.initPropertySources(this.servletContext, null);
197+
}
198+
199+
}
200+
201+
@Override
202+
public int getOrder() {
203+
return Ordered.HIGHEST_PRECEDENCE;
204+
}
205+
206+
}
207+
180208
}

spring-boot/src/test/java/org/springframework/boot/web/support/SpringBootServletInitializerTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import javax.servlet.ServletContext;
2323
import javax.servlet.ServletException;
2424

25+
import org.junit.After;
2526
import org.junit.Rule;
2627
import org.junit.Test;
2728
import org.junit.rules.ExpectedException;
@@ -33,6 +34,7 @@
3334
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
3435
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
3536
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
37+
import org.springframework.boot.testutil.InternalOutputCapture;
3638
import org.springframework.boot.web.servlet.ServletContextInitializer;
3739
import org.springframework.context.ApplicationListener;
3840
import org.springframework.context.ConfigurableApplicationContext;
@@ -59,10 +61,19 @@ public class SpringBootServletInitializerTests {
5961
@Rule
6062
public ExpectedException thrown = ExpectedException.none();
6163

64+
@Rule
65+
public InternalOutputCapture output = new InternalOutputCapture();
66+
6267
private ServletContext servletContext = new MockServletContext();
6368

6469
private SpringApplication application;
6570

71+
@After
72+
public void verifyLoggingOutput() {
73+
assertThat(this.output.toString())
74+
.doesNotContain(StandardServletEnvironment.class.getSimpleName());
75+
}
76+
6677
@Test
6778
public void failsWithoutConfigure() throws Exception {
6879
this.thrown.expect(IllegalStateException.class);

0 commit comments

Comments
 (0)