Initialize FrameworkServlet property sources eagerly

Prior to this change,
FrameworkServlet#configureAndRefreshWebApplicationContext called
 #postProcessWebApplicationContext(wac) and #applyInitializers(wac)
prior to #refresh, but because servlet-based property source stubs were
not replaced until #refresh, any post-processing or initialization
routines could not benefit from accessing the Environment to retrieve
properties from the ServletContext or ServletConfig.

The workaround to this problem is detailed in SPR-9610 - the user simply
needed to call WebApplicationContextUtils#initServletPropertySources
manually within their ApplicationContextInitializer (or overridden
 #postProcessWebApplicationContext method)

This commit ensures that
FrameworkServlet#configureAndRefreshWebApplicationContext calls
WebApplicationContextUtils#initServletPropertySources eagerly, prior to
invoking #postProcessWebApplicationContext and #applyInitializers.
Related Javadoc has also been updated throughout to clarify the behavior
of #initServletPropertySources, when it can be called and what the
effects are, etc.

Note also that a reproduction issue was added to demonstrate the problem
and verify its resolution [1].

[1]: https://github.com/SpringSource/spring-framework-issues/tree/master/SPR-9610

Issue: SPR-9610
This commit is contained in:
Chris Beams
2012-11-02 12:56:14 +01:00
parent 242bf7c4e3
commit 1070e4d5c1
4 changed files with 44 additions and 13 deletions

View File

@@ -40,6 +40,8 @@ public interface ConfigurableWebEnvironment extends ConfigurableEnvironment {
* using the given parameters.
* @param servletContext the {@link ServletContext} (may not be {@code null})
* @param servletConfig the {@link ServletConfig} ({@code null} if not available)
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(
* org.springframework.core.env.MutablePropertySources, ServletContext, ServletConfig)
*/
void initPropertySources(ServletContext servletContext, ServletConfig servletConfig);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -230,11 +230,10 @@ public abstract class WebApplicationContextUtils {
}
/**
* Replace {@code Servlet}-based stub property sources with actual instances
* populated with the given context object.
* @see org.springframework.core.env.PropertySource.StubPropertySource
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
* Convenient variant of {@link #initServletPropertySources(MutablePropertySources,
* ServletContext, ServletConfig)} that always provides {@code null} for the
* {@link ServletConfig} parameter.
* @see #initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)
*/
public static void initServletPropertySources(
MutablePropertySources propertySources, ServletContext servletContext) {
@@ -242,10 +241,20 @@ public abstract class WebApplicationContextUtils {
}
/**
* Replace {@code Servlet}-based stub property sources with actual instances
* populated with the given context and config objects.
* Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
* actual instances populated with the given {@code servletContext} and
* {@code servletConfig} objects.
* <p>This method is idempotent with respect to the fact it may be called any number
* of times but will perform replacement of stub property sources with their
* corresponding actual property sources once and only once.
* @param propertySources the {@link PropertySources} to initialize (must not be {@code null})
* @param servletContext the current {@link ServletContext} (ignored if {@code null}
* or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
* servlet context property source} has already been initialized)
* @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
* or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
* servlet config property source} has already been initialized)
* @see org.springframework.core.env.PropertySource.StubPropertySource
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
*/
public static void initServletPropertySources(