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:
parent
76bf72c9f8
commit
6db594c79d
|
|
@ -35,14 +35,12 @@ import org.springframework.core.env.PropertySource;
|
|||
* be specified using {@link JndiLocatorDelegate#setJndiEnvironment(java.util.Properties)}
|
||||
* prior to construction of the {@code JndiPropertySource}.
|
||||
*
|
||||
* <p>{@link org.springframework.web.context.support.StandardServletEnvironment
|
||||
* StandardServletEnvironment} allows for declaratively including a
|
||||
* {@code JndiPropertySource} through its support for a {@link
|
||||
* org.springframework.web.context.support.StandardServletEnvironment#JNDI_PROPERTY_SOURCE_ENABLED
|
||||
* JNDI_PROPERTY_SOURCE_ENABLED} context-param, but any customization of the underlying
|
||||
* {@link JndiLocatorDelegate} will typically be done within an {@link
|
||||
* org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}
|
||||
* or {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}.
|
||||
* <p>Note that {@link org.springframework.web.context.support.StandardServletEnvironment
|
||||
* StandardServletEnvironment} includes a {@code JndiPropertySource} by default, and any
|
||||
* customization of the underlying {@link JndiLocatorDelegate} may be performed within an
|
||||
* {@link org.springframework.context.ApplicationContextInitializer
|
||||
* ApplicationContextInitializer} or {@link org.springframework.web.WebApplicationInitializer
|
||||
* WebApplicationInitializer}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
|
|
@ -54,18 +52,6 @@ import org.springframework.core.env.PropertySource;
|
|||
*/
|
||||
public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
|
||||
|
||||
/** JNDI context property source name: {@value} */
|
||||
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiPropertySource";
|
||||
|
||||
/**
|
||||
* Create a new {@code JndiPropertySource} with the default name
|
||||
* {@value #JNDI_PROPERTY_SOURCE_NAME} and a {@link JndiLocatorDelegate} configured
|
||||
* to prefix any names with "java:comp/env/".
|
||||
*/
|
||||
public JndiPropertySource() {
|
||||
this(JNDI_PROPERTY_SOURCE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code JndiPropertySource} with the given name
|
||||
* and a {@link JndiLocatorDelegate} configured to prefix any names with
|
||||
|
|
@ -75,14 +61,6 @@ public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
|
|||
this(name, createDefaultJndiLocator());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code JndiPropertySource} with the default name
|
||||
* {@value #JNDI_PROPERTY_SOURCE_NAME} and the given {@code JndiLocatorDelegate}.
|
||||
*/
|
||||
public JndiPropertySource(JndiLocatorDelegate jndiLocator) {
|
||||
this(JNDI_PROPERTY_SOURCE_NAME, jndiLocator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code JndiPropertySource} with the given name and the given
|
||||
* {@code JndiLocatorDelegate}.
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ public class JndiPropertySourceTests {
|
|||
|
||||
@Test
|
||||
public void nonExistentProperty() {
|
||||
JndiPropertySource ps = new JndiPropertySource();
|
||||
JndiPropertySource ps = new JndiPropertySource("jndiProperties");
|
||||
assertThat(ps.getProperty("bogus"), nullValue());
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ public class JndiPropertySourceTests {
|
|||
jndiLocator.setResourceRef(true);
|
||||
jndiLocator.setJndiTemplate(jndiTemplate);
|
||||
|
||||
JndiPropertySource ps = new JndiPropertySource(jndiLocator);
|
||||
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
|
||||
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ public class JndiPropertySourceTests {
|
|||
jndiLocator.setResourceRef(true);
|
||||
jndiLocator.setJndiTemplate(jndiTemplate);
|
||||
|
||||
JndiPropertySource ps = new JndiPropertySource(jndiLocator);
|
||||
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
|
||||
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue