Skip to content

Commit 1070e4d

Browse files
committed
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
1 parent 242bf7c commit 1070e4d

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

spring-web/src/main/java/org/springframework/web/context/ConfigurableWebEnvironment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public interface ConfigurableWebEnvironment extends ConfigurableEnvironment {
4040
* using the given parameters.
4141
* @param servletContext the {@link ServletContext} (may not be {@code null})
4242
* @param servletConfig the {@link ServletConfig} ({@code null} if not available)
43+
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(
44+
* org.springframework.core.env.MutablePropertySources, ServletContext, ServletConfig)
4345
*/
4446
void initPropertySources(ServletContext servletContext, ServletConfig servletConfig);
4547

spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,22 +230,31 @@ public static void registerEnvironmentBeans(
230230
}
231231

232232
/**
233-
* Replace {@code Servlet}-based stub property sources with actual instances
234-
* populated with the given context object.
235-
* @see org.springframework.core.env.PropertySource.StubPropertySource
236-
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
237-
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
233+
* Convenient variant of {@link #initServletPropertySources(MutablePropertySources,
234+
* ServletContext, ServletConfig)} that always provides {@code null} for the
235+
* {@link ServletConfig} parameter.
236+
* @see #initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)
238237
*/
239238
public static void initServletPropertySources(
240239
MutablePropertySources propertySources, ServletContext servletContext) {
241240
initServletPropertySources(propertySources, servletContext, null);
242241
}
243242

244243
/**
245-
* Replace {@code Servlet}-based stub property sources with actual instances
246-
* populated with the given context and config objects.
244+
* Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
245+
* actual instances populated with the given {@code servletContext} and
246+
* {@code servletConfig} objects.
247+
* <p>This method is idempotent with respect to the fact it may be called any number
248+
* of times but will perform replacement of stub property sources with their
249+
* corresponding actual property sources once and only once.
250+
* @param propertySources the {@link PropertySources} to initialize (must not be {@code null})
251+
* @param servletContext the current {@link ServletContext} (ignored if {@code null}
252+
* or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
253+
* servlet context property source} has already been initialized)
254+
* @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
255+
* or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
256+
* servlet config property source} has already been initialized)
247257
* @see org.springframework.core.env.PropertySource.StubPropertySource
248-
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
249258
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
250259
*/
251260
public static void initServletPropertySources(

spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/PortletApplicationContextUtils.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -188,11 +188,25 @@ static void registerEnvironmentBeans(
188188
}
189189

190190
/**
191-
* Replace {@code Servlet}- and {@code Portlet}-based stub property sources
192-
* with actual instances populated with the given context and config objects.
191+
* Replace {@code Servlet}- and {@code Portlet}-based {@link StubPropertySource stub
192+
* property sources} with actual instances populated with the given {@code servletContext},
193+
* {@code portletContext} and {@code portletConfig} objects.
194+
* <p>This method is idempotent with respect to the fact it may be called any number
195+
* of times but will perform replacement of stub property sources with their
196+
* corresponding actual property sources once and only once.
197+
* @param propertySources the {@link PropertySources} to initialize (must not be {@code null})
198+
* @param servletContext the current {@link ServletContext} (ignored if {@code null}
199+
* or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
200+
* servlet context property source} has already been initialized)
201+
* @param portletContext the current {@link PortletContext} (ignored if {@code null}
202+
* or if the {@link StandardPortletEnvironment#PORTLET_CONTEXT_PROPERTY_SOURCE_NAME
203+
* portlet context property source} has already been initialized)
204+
* @param portletConfig the current {@link PortletConfig} (ignored if {@code null}
205+
* or if the {@link StandardPortletEnvironment#PORTLET_CONFIG_PROPERTY_SOURCE_NAME
206+
* portlet config property source} has already been initialized)
193207
* @see org.springframework.core.env.PropertySource.StubPropertySource
194-
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
195208
* @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
209+
* @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
196210
*/
197211
public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext,
198212
PortletContext portletContext, PortletConfig portletConfig) {

spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,12 @@ protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicati
634634
wac.setNamespace(getNamespace());
635635
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
636636

637+
// the wac environment's #initPropertySources will be called in any case when
638+
// the context is refreshed; do it eagerly here to ensure servlet property sources
639+
// are in place for use in any post-processing or initialization that occurs
640+
// below prior to #refresh
641+
wac.getEnvironment().initPropertySources(getServletContext(), getServletConfig());
642+
637643
postProcessWebApplicationContext(wac);
638644

639645
applyInitializers(wac);

0 commit comments

Comments
 (0)