Skip to content

Commit 30727ab

Browse files
committed
Merge branch '1.5.x'
2 parents a7ce954 + 858b092 commit 30727ab

File tree

3 files changed

+57
-61
lines changed

3 files changed

+57
-61
lines changed

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

Lines changed: 0 additions & 56 deletions
This file was deleted.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.web.WebApplicationInitializer;
4040
import org.springframework.web.context.ContextLoaderListener;
4141
import org.springframework.web.context.WebApplicationContext;
42+
import org.springframework.web.context.support.StandardServletEnvironment;
4243

4344
/**
4445
* An opinionated {@link WebApplicationInitializer} to run a {@link SpringApplication}
@@ -103,6 +104,9 @@ public void contextInitialized(ServletContextEvent event) {
103104
protected WebApplicationContext createRootApplicationContext(
104105
ServletContext servletContext) {
105106
SpringApplicationBuilder builder = createSpringApplicationBuilder();
107+
StandardServletEnvironment environment = new StandardServletEnvironment();
108+
environment.initPropertySources(servletContext, null);
109+
builder.environment(environment);
106110
builder.main(getClass());
107111
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
108112
if (parent != null) {
@@ -113,7 +117,6 @@ protected WebApplicationContext createRootApplicationContext(
113117
}
114118
builder.initializers(
115119
new ServletContextApplicationContextInitializer(servletContext));
116-
builder.listeners(new ServletContextApplicationListener(servletContext));
117120
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
118121
builder = configure(builder);
119122
SpringApplication application = builder.build();

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

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package org.springframework.boot.web.servlet.support;
1818

19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
1922
import javax.servlet.ServletContext;
23+
import javax.servlet.ServletException;
2024

2125
import org.junit.Rule;
2226
import org.junit.Test;
@@ -25,17 +29,23 @@
2529
import org.springframework.beans.DirectFieldAccessor;
2630
import org.springframework.boot.SpringApplication;
2731
import org.springframework.boot.builder.SpringApplicationBuilder;
32+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
2833
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
2934
import org.springframework.boot.web.server.WebServer;
3035
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
36+
import org.springframework.context.ApplicationListener;
3137
import org.springframework.context.ConfigurableApplicationContext;
3238
import org.springframework.context.annotation.Bean;
3339
import org.springframework.context.annotation.Configuration;
3440
import org.springframework.context.support.AbstractApplicationContext;
41+
import org.springframework.core.env.PropertySource;
3542
import org.springframework.mock.web.MockServletContext;
3643
import org.springframework.web.context.WebApplicationContext;
44+
import org.springframework.web.context.support.StandardServletEnvironment;
3745

3846
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.mockito.BDDMockito.given;
48+
import static org.mockito.Mockito.mock;
3949

4050
/**
4151
* Tests for {@link SpringBootServletInitializer}.
@@ -120,10 +130,36 @@ public void executableWarThatUsesServletInitializerDoesNotHaveErrorPageFilterCon
120130
}
121131

122132
@Test
123-
public void servletContextApplicationListenerIsAdded() {
124-
new WithConfiguredSource().createRootApplicationContext(this.servletContext);
125-
assertThat(this.application.getListeners())
126-
.hasAtLeastOneElementOfType(ServletContextApplicationListener.class);
133+
public void servletContextPropertySourceIsAvailablePriorToRefresh()
134+
throws ServletException {
135+
ServletContext servletContext = mock(ServletContext.class);
136+
given(servletContext.getInitParameterNames()).willReturn(
137+
Collections.enumeration(Arrays.asList("spring.profiles.active")));
138+
given(servletContext.getInitParameter("spring.profiles.active"))
139+
.willReturn("from-servlet-context");
140+
given(servletContext.getAttributeNames())
141+
.willReturn(Collections.enumeration(Collections.<String>emptyList()));
142+
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new PropertySourceVerifyingSpringBootServletInitializer()
143+
.createRootApplicationContext(servletContext)) {
144+
assertThat(context.getEnvironment().getActiveProfiles())
145+
.containsExactly("from-servlet-context");
146+
}
147+
}
148+
149+
private static class PropertySourceVerifyingSpringBootServletInitializer
150+
extends SpringBootServletInitializer {
151+
152+
@Override
153+
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
154+
return builder.sources(TestApp.class)
155+
.listeners(new PropertySourceVerifyingApplicationListener());
156+
}
157+
158+
}
159+
160+
@Configuration
161+
static class TestApp {
162+
127163
}
128164

129165
private class MockSpringBootServletInitializer extends SpringBootServletInitializer {
@@ -206,4 +242,17 @@ public SpringApplication build() {
206242

207243
}
208244

245+
private static final class PropertySourceVerifyingApplicationListener
246+
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
247+
248+
@Override
249+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
250+
PropertySource<?> propertySource = event.getEnvironment().getPropertySources()
251+
.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
252+
assertThat(propertySource.getProperty("spring.profiles.active"))
253+
.isEqualTo("from-servlet-context");
254+
}
255+
256+
}
257+
209258
}

0 commit comments

Comments
 (0)