Rename {DefaultWeb=>StandardServlet}Environment

Issue: SPR-8348
This commit is contained in:
Chris Beams 2011-05-20 03:55:56 +00:00
parent c06752ef72
commit f893b62a9b
14 changed files with 102 additions and 90 deletions

View File

@ -32,7 +32,7 @@ import org.springframework.core.env.PropertySource;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 3.1 * @since 3.1
* @see Context#lookup(String) * @see Context#lookup(String)
* @see org.springframework.web.context.support.DefaultWebEnvironment * @see org.springframework.web.context.support.StandardServletEnvironment
*/ */
public class JndiPropertySource extends PropertySource<Context> { public class JndiPropertySource extends PropertySource<Context> {

View File

@ -205,7 +205,7 @@ public abstract class PropertySource<T> {
* during context refresh. * during context refresh.
* *
* @see org.springframework.context.support.AbstractApplicationContext#initPropertySources() * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources()
* @see org.springframework.web.context.support.DefaultWebEnvironment * @see org.springframework.web.context.support.StandardServletEnvironment
* @see org.springframework.web.context.support.ServletContextPropertySource * @see org.springframework.web.context.support.ServletContextPropertySource
*/ */
public static class StubPropertySource extends PropertySource<Object> { public static class StubPropertySource extends PropertySource<Object> {

View File

@ -17,27 +17,28 @@
package org.springframework.core.env; package org.springframework.core.env;
/** /**
* Default implementation of the {@link Environment} interface. Used throughout all non-Web* * {@link Environment} implementation suitable for use in 'standard' (i.e. non-web)
* ApplicationContext implementations. * applications.
* *
* <p>In addition to the usual functions of a {@link ConfigurableEnvironment} such as property * <p>In addition to the usual functions of a {@link ConfigurableEnvironment} such as
* resolution and profile-related operations, this implementation configures two default property * property resolution and profile-related operations, this implementation configures two
* sources, to be searched in the following order: * default property sources, to be searched in the following order:
* <ol> * <ul>
* <li>{@linkplain AbstractEnvironment#getSystemProperties() system properties} * <li>{@linkplain AbstractEnvironment#getSystemProperties() system properties}
* <li>{@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables} * <li>{@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables}
* </ol> * </ul>
* *
* That is, if the key "xyz" is present both in the JVM system properties as well as in the * That is, if the key "xyz" is present both in the JVM system properties as well as in
* set of environment variables for the current process, the value of key "xyz" from system properties * the set of environment variables for the current process, the value of key "xyz" from
* will return from a call to {@code environment.getProperty("xyz")}. * system properties * will return from a call to {@code environment.getProperty("xyz")}.
* This ordering is chosen by default because system properties are per-JVM, while environment * This ordering is chosen by default because system properties are per-JVM, while
* variables may be the same across many JVMs on a given system. Giving system properties * environment variables may be the same across many JVMs on a given system. Giving
* precedence allows for overriding of environment variables on a per-JVM basis. * system properties precedence allows for overriding of environment variables on a
* per-JVM basis.
* *
* <p>These default property sources may be removed, reordered, or replaced; and additional * <p>These default property sources may be removed, reordered, or replaced; and
* property sources may be added using the {@link MutablePropertySources} instance available * additional property sources may be added using the {@link MutablePropertySources}
* from {@link #getPropertySources()}. * instance available from {@link #getPropertySources()}.
* *
* <h4>Example: adding a new property source with highest search priority</h4> * <h4>Example: adding a new property source with highest search priority</h4>
* <pre class="code"> * <pre class="code">
@ -62,16 +63,17 @@ package org.springframework.core.env;
* </pre> * </pre>
* *
* When an {@link Environment} is being used by an ApplicationContext, it is important * When an {@link Environment} is being used by an ApplicationContext, it is important
* that any such PropertySource manipulations be performed <em>before</em> the context's {@link * that any such PropertySource manipulations be performed <em>before</em> the context's
* org.springframework.context.support.AbstractApplicationContext#refresh() refresh()} method is * {@link org.springframework.context.support.AbstractApplicationContext#refresh()
* called. This ensures that all PropertySources are available during the container bootstrap process, * refresh()} method is called. This ensures that all PropertySources are available during
* including use by {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer * the container bootstrap process, including use by
* {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer
* property placeholder configurers}. * property placeholder configurers}.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
* @see ConfigurableEnvironment * @see ConfigurableEnvironment
* @see org.springframework.web.context.support.DefaultWebEnvironment * @see org.springframework.web.context.support.StandardServletEnvironment
*/ */
public class StandardEnvironment extends AbstractEnvironment { public class StandardEnvironment extends AbstractEnvironment {

View File

@ -69,7 +69,7 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext; import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.DefaultWebEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext;
@ -346,7 +346,7 @@ public class EnvironmentIntegrationTests {
GenericWebApplicationContext ctx = GenericWebApplicationContext ctx =
new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean()); new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
assertHasDefaultWebEnvironment(ctx); assertHasStandardServletEnvironment(ctx);
ctx.setEnvironment(prodEnv); ctx.setEnvironment(prodEnv);
ctx.refresh(); ctx.refresh();
@ -390,7 +390,7 @@ public class EnvironmentIntegrationTests {
public void staticWebApplicationContext() { public void staticWebApplicationContext() {
StaticWebApplicationContext ctx = new StaticWebApplicationContext(); StaticWebApplicationContext ctx = new StaticWebApplicationContext();
assertHasDefaultWebEnvironment(ctx); assertHasStandardServletEnvironment(ctx);
registerEnvironmentBeanDefinition(ctx); registerEnvironmentBeanDefinition(ctx);
@ -430,22 +430,22 @@ public class EnvironmentIntegrationTests {
ctx.refresh(); ctx.refresh();
ConfigurableEnvironment environment = ctx.getEnvironment(); ConfigurableEnvironment environment = ctx.getEnvironment();
assertThat(environment, instanceOf(DefaultWebEnvironment.class)); assertThat(environment, instanceOf(StandardServletEnvironment.class));
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
// ServletConfig gets precedence // ServletConfig gets precedence
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
// but all params are available // but all params are available
assertThat(environment.getProperty("pContext1"), is("pContext1Value")); assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value")); assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
// Servlet* PropertySources have precedence over System* PropertySources // Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
// Replace system properties with a mock property source for convenience // Replace system properties with a mock property source for convenience
@ -470,16 +470,16 @@ public class EnvironmentIntegrationTests {
ctx.refresh(); ctx.refresh();
ConfigurableEnvironment environment = ctx.getEnvironment(); ConfigurableEnvironment environment = ctx.getEnvironment();
assertThat(environment, instanceOf(DefaultWebEnvironment.class)); assertThat(environment, instanceOf(StandardServletEnvironment.class));
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
// ServletContext params are available // ServletContext params are available
assertThat(environment.getProperty("pCommon"), is("pCommonContextValue")); assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
assertThat(environment.getProperty("pContext1"), is("pContext1Value")); assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
// Servlet* PropertySources have precedence over System* PropertySources // Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
// Replace system properties with a mock property source for convenience // Replace system properties with a mock property source for convenience
@ -509,20 +509,20 @@ public class EnvironmentIntegrationTests {
ConfigurableEnvironment environment = ctx.getEnvironment(); ConfigurableEnvironment environment = ctx.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));
assertThat(propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true)); assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME), is(true));
// ServletConfig gets precedence // ServletConfig gets precedence
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue")); assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)))); lessThan(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
// but all params are available // but all params are available
assertThat(environment.getProperty("pContext1"), is("pContext1Value")); assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value")); assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
// Servlet* PropertySources have precedence over System* PropertySources // Servlet* PropertySources have precedence over System* PropertySources
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)))); lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
// Replace system properties with a mock property source for convenience // Replace system properties with a mock property source for convenience
@ -632,11 +632,11 @@ public class EnvironmentIntegrationTests {
assertThat(defaultEnv, instanceOf(StandardEnvironment.class)); assertThat(defaultEnv, instanceOf(StandardEnvironment.class));
} }
private void assertHasDefaultWebEnvironment(WebApplicationContext ctx) { private void assertHasStandardServletEnvironment(WebApplicationContext ctx) {
// ensure a default web environment exists // ensure a default web environment exists
Environment defaultEnv = ctx.getEnvironment(); Environment defaultEnv = ctx.getEnvironment();
assertThat(defaultEnv, notNullValue()); assertThat(defaultEnv, notNullValue());
assertThat(defaultEnv, instanceOf(DefaultWebEnvironment.class)); assertThat(defaultEnv, instanceOf(StandardServletEnvironment.class));
} }
private void assertHasDefaultPortletEnvironment(WebApplicationContext ctx) { private void assertHasDefaultPortletEnvironment(WebApplicationContext ctx) {

View File

@ -25,7 +25,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySource.StubPropertySource; import org.springframework.core.env.PropertySource.StubPropertySource;
import org.springframework.web.context.support.DefaultWebEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
/** /**
* {@link Environment} implementation to be used by {@code Servlet}-based web * {@link Environment} implementation to be used by {@code Servlet}-based web
@ -39,7 +39,7 @@ import org.springframework.web.context.support.DefaultWebEnvironment;
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
* @see StandardEnvironment * @see StandardEnvironment
* @see DefaultWebEnvironment * @see StandardServletEnvironment
*/ */
public class DefaultPortletEnvironment extends StandardEnvironment { public class DefaultPortletEnvironment extends StandardEnvironment {
@ -55,12 +55,12 @@ public class DefaultPortletEnvironment extends StandardEnvironment {
* <ul> * <ul>
* <li>{@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} * <li>{@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME}
* <li>{@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME} * <li>{@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME}
* <li>{@linkplain DefaultWebEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"} * <li>{@linkplain StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}
* </ul> * </ul>
* <p>Properties present in {@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} will * <p>Properties present in {@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} will
* take precedence over those in {@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME}, * take precedence over those in {@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME},
* which takes precedence over those in * which takes precedence over those in
* {@linkplain DefaultWebEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}. * {@linkplain StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}.
* <p>Properties in any of the above will take precedence over system properties and environment * <p>Properties in any of the above will take precedence over system properties and environment
* variables contributed by the {@link StandardEnvironment} superclass. * variables contributed by the {@link StandardEnvironment} superclass.
* <p>The property sources are added as stubs for now, and will be * <p>The property sources are added as stubs for now, and will be
@ -77,7 +77,7 @@ public class DefaultPortletEnvironment extends StandardEnvironment {
protected void customizePropertySources(MutablePropertySources propertySources) { protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME));
propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME));
propertySources.addLast(new StubPropertySource(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
super.customizePropertySources(propertySources); super.customizePropertySources(propertySources);
} }
} }

View File

@ -40,7 +40,7 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor; import org.springframework.core.io.ResourceEditor;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.support.DefaultWebEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
import org.springframework.web.context.support.ServletContextResourceLoader; import org.springframework.web.context.support.ServletContextResourceLoader;
/** /**
@ -87,7 +87,7 @@ public abstract class HttpServletBean extends HttpServlet implements Environment
*/ */
private final Set<String> requiredProperties = new HashSet<String>(); private final Set<String> requiredProperties = new HashSet<String>();
private Environment environment = new DefaultWebEnvironment(); private Environment environment = new StandardServletEnvironment();
/** /**
@ -182,7 +182,7 @@ public abstract class HttpServletBean extends HttpServlet implements Environment
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p>Any environment set here overrides the {@link DefaultWebEnvironment} * <p>Any environment set here overrides the {@link StandardServletEnvironment}
* provided by default. * provided by default.
*/ */
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {

View File

@ -132,11 +132,11 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR
} }
/** /**
* Create and return a new {@link DefaultWebEnvironment}. * Create and return a new {@link StandardServletEnvironment}.
*/ */
@Override @Override
protected ConfigurableEnvironment createEnvironment() { protected ConfigurableEnvironment createEnvironment() {
return new DefaultWebEnvironment(); return new StandardServletEnvironment();
} }

View File

@ -118,11 +118,11 @@ public class GenericWebApplicationContext extends GenericApplicationContext
/** /**
* Create and return a new {@link DefaultWebEnvironment}. * Create and return a new {@link StandardServletEnvironment}.
*/ */
@Override @Override
protected ConfigurableEnvironment createEnvironment() { protected ConfigurableEnvironment createEnvironment() {
return new DefaultWebEnvironment(); return new StandardServletEnvironment();
} }
/** /**

View File

@ -59,7 +59,7 @@ import org.springframework.web.context.ServletContextAware;
* @see javax.servlet.ServletContext#getInitParameter(String) * @see javax.servlet.ServletContext#getInitParameter(String)
* @see javax.servlet.ServletContext#getAttribute(String) * @see javax.servlet.ServletContext#getAttribute(String)
* @deprecated in Spring 3.1 in favor of {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer} * @deprecated in Spring 3.1 in favor of {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer}
* in conjunction with {@link org.springframework.web.context.support.DefaultWebEnvironment}. * in conjunction with {@link org.springframework.web.context.support.StandardServletEnvironment}.
*/ */
@Deprecated @Deprecated
public class ServletContextPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer public class ServletContextPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer

View File

@ -32,20 +32,22 @@ import org.springframework.jndi.JndiPropertySource;
* applications. All web-related (servlet-based) {@code ApplicationContext} classes * applications. All web-related (servlet-based) {@code ApplicationContext} classes
* initialize an instance by default. * initialize an instance by default.
* *
* <p>Contributes {@code ServletConfig}- and {@code ServletContext}-based {@link PropertySource} * <p>Contributes {@code ServletConfig}- and {@code ServletContext}-based
* instances. See the {@link #DefaultWebEnvironment()} constructor for details. * {@link PropertySource} instances. See the {@link #customizePropertySources} method
* for details.
* *
* <p>After initial bootstrapping, property sources will be searched for the presence of a * <p>After initial bootstrapping, property sources will be searched for the presence of a
* "jndiPropertySourceEnabled" property; if found, a {@link JndiPropertySource} will be added * "jndiPropertySourceEnabled" property; if found, a {@link JndiPropertySource} will be
* to this environment's {@link PropertySources}, with precedence higher than system properties * added to this environment's {@link PropertySources}, with precedence higher than system
* and environment variables, but lower than that of ServletContext and ServletConfig init params. * properties and environment variables, but lower than that of ServletContext and
* ServletConfig init params.
* *
* @author Chris Beams * @author Chris Beams
* @since 3.1 * @since 3.1
* @see StandardEnvironment * @see StandardEnvironment
* @see DefaultPortletEnvironment * @see DefaultPortletEnvironment
*/ */
public class DefaultWebEnvironment extends StandardEnvironment { public class StandardServletEnvironment extends StandardEnvironment {
/** Servlet context init parameters property source name: {@value} */ /** Servlet context init parameters property source name: {@value} */
public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams"; public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";
@ -63,15 +65,17 @@ public class DefaultWebEnvironment extends StandardEnvironment {
* </ul> * </ul>
* <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
* take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}. * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}.
* <p>Properties in any of the above will take precedence over system properties and environment * <p>Properties in any of the above will take precedence over system properties and
* variables contributed by the {@link StandardEnvironment} superclass. * environment variables contributed by the {@link StandardEnvironment} superclass.
* <p>The {@code Servlet}-related property sources are added as stubs for now, and will be * <p>The {@code Servlet}-related property sources are added as stubs for now, and
* {@linkplain WebApplicationContextUtils#initServletPropertySources fully initialized} * will be {@linkplain WebApplicationContextUtils#initServletPropertySources fully
* once the actual {@link ServletConfig} and {@link ServletContext} objects are available. * initialized} once the actual {@link ServletConfig} and {@link ServletContext}
* objects are available.
* <p>If the {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_ENABLED_FLAG "jndiPropertySourceEnabled"} * <p>If the {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_ENABLED_FLAG "jndiPropertySourceEnabled"}
* property is present in any of the default property sources, a {@link JndiPropertySource} will * property is present in any of the default property sources, a
* be added as well, with precedence lower than servlet property sources, but higher than system * {@link JndiPropertySource} will be added as well, with precedence lower than
* properties and environment variables. * servlet property sources, but higher than system properties and environment
* variables.
* @see StandardEnvironment#customizePropertySources * @see StandardEnvironment#customizePropertySources
* @see ServletConfigPropertySource * @see ServletConfigPropertySource
* @see ServletContextPropertySource * @see ServletContextPropertySource

View File

@ -160,11 +160,11 @@ public class StaticWebApplicationContext extends StaticApplicationContext
} }
/** /**
* Create and return a new {@link DefaultWebEnvironment}. * Create and return a new {@link StandardServletEnvironment}.
*/ */
@Override @Override
protected ConfigurableEnvironment createEnvironment() { protected ConfigurableEnvironment createEnvironment() {
return new DefaultWebEnvironment(); return new StandardServletEnvironment();
} }
/** /**

View File

@ -247,13 +247,13 @@ public abstract class WebApplicationContextUtils {
public static void initServletPropertySources( public static void initServletPropertySources(
MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) { MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
Assert.notNull(propertySources, "propertySources must not be null"); Assert.notNull(propertySources, "propertySources must not be null");
if(servletContext != null && propertySources.contains(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)) { if(servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)) {
propertySources.replace(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
new ServletContextPropertySource(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext)); new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
} }
if(servletConfig != null && propertySources.contains(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)) { if(servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)) {
propertySources.replace(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
new ServletConfigPropertySource(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig)); new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
} }
} }

View File

@ -44,7 +44,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.DefaultWebEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
import org.springframework.web.context.support.ServletContextResourceLoader; import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.util.NestedServletException; import org.springframework.web.util.NestedServletException;
@ -91,7 +91,7 @@ public abstract class GenericFilterBean implements
private String beanName; private String beanName;
private Environment environment = new DefaultWebEnvironment(); private Environment environment = new StandardServletEnvironment();
private ServletContext servletContext; private ServletContext servletContext;
@ -109,7 +109,7 @@ public abstract class GenericFilterBean implements
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p>Any environment set here overrides the {@link DefaultWebEnvironment} * <p>Any environment set here overrides the {@link StandardServletEnvironment}
* provided by default. * provided by default.
*/ */
public void setEnvironment(Environment environment) { public void setEnvironment(Environment environment) {

View File

@ -27,14 +27,20 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.jndi.JndiPropertySource; import org.springframework.jndi.JndiPropertySource;
public class DefaultWebEnvironmentTests { /**
* Unit tests for {@link StandardServletEnvironment}.
*
* @author Chris Beams
* @since 3.1
*/
public class StandardServletEnvironmentTests {
@Test @Test
public void propertySourceOrder() { public void propertySourceOrder() {
ConfigurableEnvironment env = new DefaultWebEnvironment(); ConfigurableEnvironment env = new StandardServletEnvironment();
MutablePropertySources sources = env.getPropertySources(); MutablePropertySources sources = env.getPropertySources();
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
assertThat(sources.size(), is(4)); assertThat(sources.size(), is(4));
@ -43,11 +49,11 @@ public class DefaultWebEnvironmentTests {
@Test @Test
public void propertySourceOrder_jndiPropertySourceEnabled() { public void propertySourceOrder_jndiPropertySourceEnabled() {
System.setProperty(JndiPropertySource.JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "true"); System.setProperty(JndiPropertySource.JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "true");
ConfigurableEnvironment env = new DefaultWebEnvironment(); ConfigurableEnvironment env = new StandardServletEnvironment();
MutablePropertySources sources = env.getPropertySources(); MutablePropertySources sources = env.getPropertySources();
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4));
@ -59,11 +65,11 @@ public class DefaultWebEnvironmentTests {
@Test @Test
public void propertySourceOrder_jndiPropertySourceEnabledIsFalse() { public void propertySourceOrder_jndiPropertySourceEnabledIsFalse() {
System.setProperty(JndiPropertySource.JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "false"); System.setProperty(JndiPropertySource.JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "false");
ConfigurableEnvironment env = new DefaultWebEnvironment(); ConfigurableEnvironment env = new StandardServletEnvironment();
MutablePropertySources sources = env.getPropertySources(); MutablePropertySources sources = env.getPropertySources();
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1)); assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
//assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2)); //assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3)); assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));