Merge branch '1.5.x'
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.web.servlet.support;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.context.config.ConfigFileApplicationListener;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.context.ConfigurableWebEnvironment;
|
||||
|
||||
/**
|
||||
* An {@link ApplicationListener} that initializes the {@link SpringApplication} using the
|
||||
* {@link ServletContext}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
final class ServletContextApplicationListener
|
||||
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
|
||||
|
||||
private final ServletContext servletContext;
|
||||
|
||||
ServletContextApplicationListener(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return ConfigFileApplicationListener.DEFAULT_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||
if (event.getEnvironment() instanceof ConfigurableWebEnvironment) {
|
||||
((ConfigurableWebEnvironment) event.getEnvironment())
|
||||
.initPropertySources(this.servletContext, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,6 +39,7 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.StandardServletEnvironment;
|
||||
|
||||
/**
|
||||
* An opinionated {@link WebApplicationInitializer} to run a {@link SpringApplication}
|
||||
@@ -103,6 +104,9 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
|
||||
protected WebApplicationContext createRootApplicationContext(
|
||||
ServletContext servletContext) {
|
||||
SpringApplicationBuilder builder = createSpringApplicationBuilder();
|
||||
StandardServletEnvironment environment = new StandardServletEnvironment();
|
||||
environment.initPropertySources(servletContext, null);
|
||||
builder.environment(environment);
|
||||
builder.main(getClass());
|
||||
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
|
||||
if (parent != null) {
|
||||
@@ -113,7 +117,6 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
|
||||
}
|
||||
builder.initializers(
|
||||
new ServletContextApplicationContextInitializer(servletContext));
|
||||
builder.listeners(new ServletContextApplicationListener(servletContext));
|
||||
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
|
||||
builder = configure(builder);
|
||||
SpringApplication application = builder.build();
|
||||
|
||||
@@ -16,7 +16,11 @@
|
||||
|
||||
package org.springframework.boot.web.servlet.support;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -25,17 +29,23 @@ import org.junit.rules.ExpectedException;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
|
||||
import org.springframework.boot.web.server.WebServer;
|
||||
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.StandardServletEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringBootServletInitializer}.
|
||||
@@ -120,10 +130,36 @@ public class SpringBootServletInitializerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void servletContextApplicationListenerIsAdded() {
|
||||
new WithConfiguredSource().createRootApplicationContext(this.servletContext);
|
||||
assertThat(this.application.getListeners())
|
||||
.hasAtLeastOneElementOfType(ServletContextApplicationListener.class);
|
||||
public void servletContextPropertySourceIsAvailablePriorToRefresh()
|
||||
throws ServletException {
|
||||
ServletContext servletContext = mock(ServletContext.class);
|
||||
given(servletContext.getInitParameterNames()).willReturn(
|
||||
Collections.enumeration(Arrays.asList("spring.profiles.active")));
|
||||
given(servletContext.getInitParameter("spring.profiles.active"))
|
||||
.willReturn("from-servlet-context");
|
||||
given(servletContext.getAttributeNames())
|
||||
.willReturn(Collections.enumeration(Collections.<String>emptyList()));
|
||||
try (ConfigurableApplicationContext context = (ConfigurableApplicationContext) new PropertySourceVerifyingSpringBootServletInitializer()
|
||||
.createRootApplicationContext(servletContext)) {
|
||||
assertThat(context.getEnvironment().getActiveProfiles())
|
||||
.containsExactly("from-servlet-context");
|
||||
}
|
||||
}
|
||||
|
||||
private static class PropertySourceVerifyingSpringBootServletInitializer
|
||||
extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
|
||||
return builder.sources(TestApp.class)
|
||||
.listeners(new PropertySourceVerifyingApplicationListener());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestApp {
|
||||
|
||||
}
|
||||
|
||||
private class MockSpringBootServletInitializer extends SpringBootServletInitializer {
|
||||
@@ -206,4 +242,17 @@ public class SpringBootServletInitializerTests {
|
||||
|
||||
}
|
||||
|
||||
private static final class PropertySourceVerifyingApplicationListener
|
||||
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||
PropertySource<?> propertySource = event.getEnvironment().getPropertySources()
|
||||
.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
|
||||
assertThat(propertySource.getProperty("spring.profiles.active"))
|
||||
.isEqualTo("from-servlet-context");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user