Make servlet context property source available before refresh
Previously, when deploying a Spring Boot application to a container, the servlet context property source was not fully initialised until the context was refreshed. This led to a problem where a value from a property source with lower precedence would be seen during the early stages of the application starting. Once the servlet context property source had been initialized, its value for the property would then become visible effectively making it appear as if the property's value had changed during startup. This led to a specific problem with determining active profiles. If spring.profiles.active was set both in JNDI and via the servlet context both profiles would end up being active, rather than the more intuitive behaviour of the profiles made active via the servlet context overriding those made active via JNDI. This commit updates SpringBootServletInitializer so that it explicitly creates the StandardServletEnvironment and initializes its property sources using the servlet context. This is done before the application is created and run, thereby ensuring that the servlet context property source is available throughout the application's startup. Closes gh-9972pull/9712/merge
parent
71dbbc0d66
commit
858b092a87
@ -1,56 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2012-2016 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.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue