Register JndiPropertySource by default in servlet environments
Prior to this change, StandardServletEnvironment evaluated a "jndiPropertySourceEnabled" flag to determine whether or not to add a JndiPropertySource. Following the changes introduced in SPR-8490, there is now no reason not to enable a JNDI property source by default. This change eliminates the support for "jndiPropertySourceEnabled" and adds a JndiPropertySource automatically. Issue: SPR-8545, SPR-8490
This commit is contained in:
@@ -19,12 +19,11 @@ package org.springframework.web.context.support;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.env.PropertySource.StubPropertySource;
|
||||
import org.springframework.core.env.PropertySources;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.jndi.JndiPropertySource;
|
||||
|
||||
/**
|
||||
@@ -32,20 +31,13 @@ import org.springframework.jndi.JndiPropertySource;
|
||||
* applications. All web-related (servlet-based) {@code ApplicationContext} classes
|
||||
* initialize an instance by default.
|
||||
*
|
||||
* <p>Contributes {@code ServletConfig}- and {@code ServletContext}-based
|
||||
* {@link PropertySource} instances. See the {@link #customizePropertySources} method
|
||||
* for details.
|
||||
*
|
||||
* <p>After initial bootstrapping, property sources will be searched for the presence of a
|
||||
* "jndiPropertySourceEnabled" property; if found, a {@link JndiPropertySource} will be
|
||||
* added to this environment's {@link PropertySources}, with precedence higher than system
|
||||
* properties and environment variables, but lower than that of ServletContext and
|
||||
* ServletConfig init params.
|
||||
* <p>Contributes {@code ServletConfig}, {@code ServletContext}, and JNDI-based
|
||||
* {@link PropertySource} instances. See {@link #customizePropertySources} method
|
||||
* documentation for details.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see StandardEnvironment
|
||||
* @see StandardPortletEnvironment
|
||||
*/
|
||||
public class StandardServletEnvironment extends StandardEnvironment {
|
||||
|
||||
@@ -55,11 +47,8 @@ public class StandardServletEnvironment extends StandardEnvironment {
|
||||
/** Servlet config init parameters property source name: {@value} */
|
||||
public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";
|
||||
|
||||
/**
|
||||
* Name of property used to determine if a {@link JndiPropertySource}
|
||||
* should be registered by default: {@value}
|
||||
*/
|
||||
public static final String JNDI_PROPERTY_SOURCE_ENABLED_FLAG = "jndiPropertySourceEnabled";
|
||||
/** JNDI property source name: {@value} */
|
||||
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
|
||||
|
||||
|
||||
/**
|
||||
@@ -68,21 +57,18 @@ public class StandardServletEnvironment extends StandardEnvironment {
|
||||
* <ul>
|
||||
* <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
|
||||
* <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
|
||||
* <li>(optionally) {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_NAME "jndiPropertySource"}
|
||||
* <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
|
||||
* </ul>
|
||||
* <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}, and
|
||||
* properties found in either of the above take precedence over those found in
|
||||
* {@value #JNDI_PROPERTY_SOURCE_NAME}.
|
||||
* <p>Properties in any of the above will take precedence over system properties and
|
||||
* environment variables contributed by the {@link StandardEnvironment} superclass.
|
||||
* <p>The {@code Servlet}-related property sources are added as stubs for now, and
|
||||
* will be {@linkplain WebApplicationContextUtils#initServletPropertySources fully
|
||||
* initialized} once the actual {@link ServletConfig} and {@link ServletContext}
|
||||
* objects are available.
|
||||
* <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 be added as well, with precedence lower than
|
||||
* servlet property sources, but higher than system properties and environment
|
||||
* variables.
|
||||
* @see StandardEnvironment#customizePropertySources
|
||||
* @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
|
||||
* @see ServletConfigPropertySource
|
||||
@@ -95,10 +81,7 @@ public class StandardServletEnvironment extends StandardEnvironment {
|
||||
protected void customizePropertySources(MutablePropertySources propertySources) {
|
||||
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
|
||||
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
|
||||
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
|
||||
super.customizePropertySources(propertySources);
|
||||
|
||||
if (this.getProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, boolean.class, false)) {
|
||||
propertySources.addAfter(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new JndiPropertySource());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,14 +19,12 @@ package org.springframework.web.context.support;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.web.context.support.StandardServletEnvironment.JNDI_PROPERTY_SOURCE_ENABLED_FLAG;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.jndi.JndiPropertySource;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link StandardServletEnvironment}.
|
||||
@@ -40,42 +38,13 @@ public class StandardServletEnvironmentTests {
|
||||
public void propertySourceOrder() {
|
||||
ConfigurableEnvironment env = new StandardServletEnvironment();
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
|
||||
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_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
|
||||
assertThat(sources.size(), is(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertySourceOrder_jndiPropertySourceEnabled() {
|
||||
System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "true");
|
||||
ConfigurableEnvironment env = new StandardServletEnvironment();
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
|
||||
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
|
||||
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(StandardServletEnvironment.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_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4));
|
||||
assertThat(sources.size(), is(5));
|
||||
|
||||
System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertySourceOrder_jndiPropertySourceEnabledIsFalse() {
|
||||
System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "false");
|
||||
ConfigurableEnvironment env = new StandardServletEnvironment();
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
|
||||
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
|
||||
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(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
|
||||
assertThat(sources.size(), is(4));
|
||||
|
||||
System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user