Skip to content

Commit 6db594c

Browse files
committed
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
1 parent 76bf72c commit 6db594c

File tree

4 files changed

+21
-91
lines changed

4 files changed

+21
-91
lines changed

org.springframework.context/src/main/java/org/springframework/jndi/JndiPropertySource.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@
3535
* be specified using {@link JndiLocatorDelegate#setJndiEnvironment(java.util.Properties)}
3636
* prior to construction of the {@code JndiPropertySource}.
3737
*
38-
* <p>{@link org.springframework.web.context.support.StandardServletEnvironment
39-
* StandardServletEnvironment} allows for declaratively including a
40-
* {@code JndiPropertySource} through its support for a {@link
41-
* org.springframework.web.context.support.StandardServletEnvironment#JNDI_PROPERTY_SOURCE_ENABLED
42-
* JNDI_PROPERTY_SOURCE_ENABLED} context-param, but any customization of the underlying
43-
* {@link JndiLocatorDelegate} will typically be done within an {@link
44-
* org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}
45-
* or {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}.
38+
* <p>Note that {@link org.springframework.web.context.support.StandardServletEnvironment
39+
* StandardServletEnvironment} includes a {@code JndiPropertySource} by default, and any
40+
* customization of the underlying {@link JndiLocatorDelegate} may be performed within an
41+
* {@link org.springframework.context.ApplicationContextInitializer
42+
* ApplicationContextInitializer} or {@link org.springframework.web.WebApplicationInitializer
43+
* WebApplicationInitializer}.
4644
*
4745
* @author Chris Beams
4846
* @author Juergen Hoeller
@@ -54,18 +52,6 @@
5452
*/
5553
public class JndiPropertySource extends PropertySource<JndiLocatorDelegate> {
5654

57-
/** JNDI context property source name: {@value} */
58-
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiPropertySource";
59-
60-
/**
61-
* Create a new {@code JndiPropertySource} with the default name
62-
* {@value #JNDI_PROPERTY_SOURCE_NAME} and a {@link JndiLocatorDelegate} configured
63-
* to prefix any names with "java:comp/env/".
64-
*/
65-
public JndiPropertySource() {
66-
this(JNDI_PROPERTY_SOURCE_NAME);
67-
}
68-
6955
/**
7056
* Create a new {@code JndiPropertySource} with the given name
7157
* and a {@link JndiLocatorDelegate} configured to prefix any names with
@@ -75,14 +61,6 @@ public JndiPropertySource(String name) {
7561
this(name, createDefaultJndiLocator());
7662
}
7763

78-
/**
79-
* Create a new {@code JndiPropertySource} with the default name
80-
* {@value #JNDI_PROPERTY_SOURCE_NAME} and the given {@code JndiLocatorDelegate}.
81-
*/
82-
public JndiPropertySource(JndiLocatorDelegate jndiLocator) {
83-
this(JNDI_PROPERTY_SOURCE_NAME, jndiLocator);
84-
}
85-
8664
/**
8765
* Create a new {@code JndiPropertySource} with the given name and the given
8866
* {@code JndiLocatorDelegate}.

org.springframework.context/src/test/java/org/springframework/jndi/JndiPropertySourceTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class JndiPropertySourceTests {
3636

3737
@Test
3838
public void nonExistentProperty() {
39-
JndiPropertySource ps = new JndiPropertySource();
39+
JndiPropertySource ps = new JndiPropertySource("jndiProperties");
4040
assertThat(ps.getProperty("bogus"), nullValue());
4141
}
4242

@@ -55,7 +55,7 @@ protected Context createInitialContext() throws NamingException {
5555
jndiLocator.setResourceRef(true);
5656
jndiLocator.setJndiTemplate(jndiTemplate);
5757

58-
JndiPropertySource ps = new JndiPropertySource(jndiLocator);
58+
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
5959
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
6060
}
6161

@@ -74,7 +74,7 @@ protected Context createInitialContext() throws NamingException {
7474
jndiLocator.setResourceRef(true);
7575
jndiLocator.setJndiTemplate(jndiTemplate);
7676

77-
JndiPropertySource ps = new JndiPropertySource(jndiLocator);
77+
JndiPropertySource ps = new JndiPropertySource("jndiProperties", jndiLocator);
7878
assertThat((String)ps.getProperty("p1"), equalTo("v1"));
7979
}
8080

org.springframework.web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,25 @@
1919
import javax.servlet.ServletConfig;
2020
import javax.servlet.ServletContext;
2121

22-
import org.springframework.core.env.StandardEnvironment;
2322
import org.springframework.core.env.Environment;
2423
import org.springframework.core.env.MutablePropertySources;
2524
import org.springframework.core.env.PropertySource;
2625
import org.springframework.core.env.PropertySource.StubPropertySource;
27-
import org.springframework.core.env.PropertySources;
26+
import org.springframework.core.env.StandardEnvironment;
2827
import org.springframework.jndi.JndiPropertySource;
2928

3029
/**
3130
* {@link Environment} implementation to be used by {@code Servlet}-based web
3231
* applications. All web-related (servlet-based) {@code ApplicationContext} classes
3332
* initialize an instance by default.
3433
*
35-
* <p>Contributes {@code ServletConfig}- and {@code ServletContext}-based
36-
* {@link PropertySource} instances. See the {@link #customizePropertySources} method
37-
* for details.
38-
*
39-
* <p>After initial bootstrapping, property sources will be searched for the presence of a
40-
* "jndiPropertySourceEnabled" property; if found, a {@link JndiPropertySource} will be
41-
* added to this environment's {@link PropertySources}, with precedence higher than system
42-
* properties and environment variables, but lower than that of ServletContext and
43-
* ServletConfig init params.
34+
* <p>Contributes {@code ServletConfig}, {@code ServletContext}, and JNDI-based
35+
* {@link PropertySource} instances. See {@link #customizePropertySources} method
36+
* documentation for details.
4437
*
4538
* @author Chris Beams
4639
* @since 3.1
4740
* @see StandardEnvironment
48-
* @see StandardPortletEnvironment
4941
*/
5042
public class StandardServletEnvironment extends StandardEnvironment {
5143

@@ -55,11 +47,8 @@ public class StandardServletEnvironment extends StandardEnvironment {
5547
/** Servlet config init parameters property source name: {@value} */
5648
public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";
5749

58-
/**
59-
* Name of property used to determine if a {@link JndiPropertySource}
60-
* should be registered by default: {@value}
61-
*/
62-
public static final String JNDI_PROPERTY_SOURCE_ENABLED_FLAG = "jndiPropertySourceEnabled";
50+
/** JNDI property source name: {@value} */
51+
public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
6352

6453

6554
/**
@@ -68,21 +57,18 @@ public class StandardServletEnvironment extends StandardEnvironment {
6857
* <ul>
6958
* <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
7059
* <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
71-
* <li>(optionally) {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_NAME "jndiPropertySource"}
60+
* <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
7261
* </ul>
7362
* <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
74-
* take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}.
63+
* take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
64+
* properties found in either of the above take precedence over those found in
65+
* {@value #JNDI_PROPERTY_SOURCE_NAME}.
7566
* <p>Properties in any of the above will take precedence over system properties and
7667
* environment variables contributed by the {@link StandardEnvironment} superclass.
7768
* <p>The {@code Servlet}-related property sources are added as stubs for now, and
7869
* will be {@linkplain WebApplicationContextUtils#initServletPropertySources fully
7970
* initialized} once the actual {@link ServletConfig} and {@link ServletContext}
8071
* objects are available.
81-
* <p>If the {@link JndiPropertySource#JNDI_PROPERTY_SOURCE_ENABLED_FLAG "jndiPropertySourceEnabled"}
82-
* property is present in any of the default property sources, a
83-
* {@link JndiPropertySource} will be added as well, with precedence lower than
84-
* servlet property sources, but higher than system properties and environment
85-
* variables.
8672
* @see StandardEnvironment#customizePropertySources
8773
* @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
8874
* @see ServletConfigPropertySource
@@ -95,10 +81,7 @@ public class StandardServletEnvironment extends StandardEnvironment {
9581
protected void customizePropertySources(MutablePropertySources propertySources) {
9682
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
9783
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
84+
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
9885
super.customizePropertySources(propertySources);
99-
100-
if (this.getProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, boolean.class, false)) {
101-
propertySources.addAfter(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new JndiPropertySource());
102-
}
10386
}
10487
}

org.springframework.web/src/test/java/org/springframework/web/context/support/StandardServletEnvironmentTests.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
import static org.hamcrest.CoreMatchers.equalTo;
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.junit.Assert.assertThat;
22-
import static org.springframework.web.context.support.StandardServletEnvironment.JNDI_PROPERTY_SOURCE_ENABLED_FLAG;
2322

2423
import org.junit.Test;
2524
import org.springframework.core.env.ConfigurableEnvironment;
2625
import org.springframework.core.env.StandardEnvironment;
2726
import org.springframework.core.env.MutablePropertySources;
2827
import org.springframework.core.env.PropertySource;
29-
import org.springframework.jndi.JndiPropertySource;
3028

3129
/**
3230
* Unit tests for {@link StandardServletEnvironment}.
@@ -40,42 +38,13 @@ public class StandardServletEnvironmentTests {
4038
public void propertySourceOrder() {
4139
ConfigurableEnvironment env = new StandardServletEnvironment();
4240
MutablePropertySources sources = env.getPropertySources();
43-
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
44-
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
45-
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
46-
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
47-
assertThat(sources.size(), is(4));
48-
}
49-
50-
@Test
51-
public void propertySourceOrder_jndiPropertySourceEnabled() {
52-
System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "true");
53-
ConfigurableEnvironment env = new StandardServletEnvironment();
54-
MutablePropertySources sources = env.getPropertySources();
5541

5642
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
5743
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
58-
assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
44+
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
5945
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3));
6046
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4));
6147
assertThat(sources.size(), is(5));
62-
63-
System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG);
6448
}
6549

66-
@Test
67-
public void propertySourceOrder_jndiPropertySourceEnabledIsFalse() {
68-
System.setProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG, "false");
69-
ConfigurableEnvironment env = new StandardServletEnvironment();
70-
MutablePropertySources sources = env.getPropertySources();
71-
72-
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
73-
assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
74-
//assertThat(sources.precedenceOf(PropertySource.named(JndiPropertySource.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
75-
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
76-
assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
77-
assertThat(sources.size(), is(4));
78-
79-
System.clearProperty(JNDI_PROPERTY_SOURCE_ENABLED_FLAG);
80-
}
8150
}

0 commit comments

Comments
 (0)