Introduce and integrate JndiPropertySource

DefaultWebEnvironment automatically adds a JndiPropertySource if
a "jndiPropertySourceEnabled" property is detected in any of the
other other default property sources.
This commit is contained in:
Chris Beams
2011-01-06 07:43:29 +00:00
parent a7704c8cce
commit 15ac99f59c
4 changed files with 162 additions and 0 deletions

View File

@@ -20,8 +20,11 @@ import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import org.springframework.core.env.DefaultEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySource.StubPropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.jndi.JndiPropertySource;
/**
* {@link Environment} implementation to be used by {@code Servlet}-based web
@@ -31,6 +34,11 @@ import org.springframework.core.env.PropertySource.StubPropertySource;
* <p>Contributes {@code ServletConfig}- and {@code ServletContext}-based {@link PropertySource}
* instances. See the {@link #DefaultWebEnvironment()} constructor 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.
*
* @author Chris Beams
* @since 3.1
* @see DefaultEnvironment
@@ -50,6 +58,7 @@ public class DefaultWebEnvironment extends DefaultEnvironment {
* <ul>
* <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
* <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
* <li>(optionally) {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_NAME "jndiPropertySource"}
* </ul>
* <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
* take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}.
@@ -58,14 +67,25 @@ public class DefaultWebEnvironment extends DefaultEnvironment {
* <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 DefaultEnvironment#DefaultEnvironment
* @see ServletConfigPropertySource
* @see ServletContextPropertySource
* @see org.springframework.jndi.JndiPropertySource
* @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
* @see WebApplicationContextUtils#initServletPropertySources
*/
public DefaultWebEnvironment() {
this.getPropertySources().addFirst(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
this.getPropertySources().addFirst(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
Boolean jndiPropertySourceEnabled = this.getProperty(JndiPropertySource.JNDI_PROPERTY_SOURCE_ENABLED_FLAG, boolean.class);
if (jndiPropertySourceEnabled != null && jndiPropertySourceEnabled != false) {
this.getPropertySources().addAfter(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new JndiPropertySource());
}
}
}