Skip to content

Commit 949c227

Browse files
committed
Restore ApplicationServletEnvironment in web deployments
Closes gh-48254
2 parents 635e331 + 709e19f commit 949c227

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.boot.web.context.servlet.WebApplicationContextInitializer;
4444
import org.springframework.boot.web.servlet.ServletContextInitializer;
4545
import org.springframework.context.ApplicationContext;
46+
import org.springframework.context.ApplicationContextException;
4647
import org.springframework.context.ApplicationListener;
4748
import org.springframework.context.ConfigurableApplicationContext;
4849
import org.springframework.context.annotation.Configuration;
@@ -295,6 +296,17 @@ private WarDeploymentApplicationContextFactory(ServletContext servletContext) {
295296
this.servletContext = servletContext;
296297
}
297298

299+
@Override
300+
public @Nullable Class<? extends ConfigurableEnvironment> getEnvironmentType(
301+
@Nullable WebApplicationType webApplicationType) {
302+
return (webApplicationType != WebApplicationType.SERVLET) ? null : ApplicationServletEnvironment.class;
303+
}
304+
305+
@Override
306+
public ConfigurableEnvironment createEnvironment(@Nullable WebApplicationType webApplicationType) {
307+
return new ApplicationServletEnvironment();
308+
}
309+
298310
@Override
299311
public ConfigurableApplicationContext create(@Nullable WebApplicationType webApplicationType) {
300312
return new AnnotationConfigServletWebApplicationContext() {
@@ -307,18 +319,13 @@ protected void onRefresh() {
307319
.initialize(WarDeploymentApplicationContextFactory.this.servletContext);
308320
}
309321
catch (ServletException ex) {
310-
throw new RuntimeException(ex);
322+
throw new ApplicationContextException("Cannot initialize servlet context", ex);
311323
}
312324
}
313325

314326
};
315327
}
316328

317-
@Override
318-
public ConfigurableEnvironment createEnvironment(@Nullable WebApplicationType webApplicationType) {
319-
return new ApplicationServletEnvironment();
320-
}
321-
322329
}
323330

324331
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.context.annotation.Configuration;
4848
import org.springframework.context.support.AbstractApplicationContext;
4949
import org.springframework.core.Ordered;
50+
import org.springframework.core.env.ConfigurableEnvironment;
5051
import org.springframework.core.env.PropertySource;
5152
import org.springframework.mock.web.MockServletContext;
5253
import org.springframework.test.util.ReflectionTestUtils;
@@ -177,6 +178,29 @@ void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterConfigured
177178
}
178179
}
179180

181+
@Test
182+
void environmentIsConfiguredWithStandardServletEnvironment() {
183+
ServletContext servletContext = mock(ServletContext.class);
184+
given(servletContext.addFilter(any(), any(Filter.class))).willReturn(mock(Dynamic.class));
185+
given(servletContext.getInitParameterNames())
186+
.willReturn(Collections.enumeration(Collections.singletonList("servlet.init.test")));
187+
given(servletContext.getInitParameter("servlet.init.test")).willReturn("from-servlet-context");
188+
given(servletContext.getAttributeNames())
189+
.willReturn(Collections.enumeration(Collections.singletonList("servlet.attribute.test")));
190+
given(servletContext.getAttribute("servlet.attribute.test")).willReturn("also-from-servlet-context");
191+
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new RegularSpringBootServletInitializer()
192+
.createRootApplicationContext(servletContext)) {
193+
assertThat(context).isNotNull();
194+
ConfigurableEnvironment environment = context.getEnvironment();
195+
assertThat(environment).isInstanceOf(StandardServletEnvironment.class);
196+
assertThat(environment.getClass().getName()).endsWith("ApplicationServletEnvironment");
197+
assertThat(environment.getPropertySources()).map(PropertySource::getName)
198+
.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
199+
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
200+
assertThat(environment.getProperty("servlet.init.test")).isEqualTo("from-servlet-context");
201+
}
202+
}
203+
180204
@Test
181205
void servletContextPropertySourceIsAvailablePriorToRefresh() {
182206
ServletContext servletContext = mock(ServletContext.class);
@@ -334,6 +358,15 @@ public SpringApplication build() {
334358

335359
}
336360

361+
static class RegularSpringBootServletInitializer extends SpringBootServletInitializer {
362+
363+
@Override
364+
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
365+
return builder.sources(TestApp.class);
366+
}
367+
368+
}
369+
337370
private static final class PropertySourceVerifyingApplicationListener
338371
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
339372

0 commit comments

Comments
 (0)