Skip to content

Commit a23629f

Browse files
committed
Context namespace exposes value-separator attribute for property-placeholder element
Issue: SPR-7794
1 parent c7fd4cc commit a23629f

File tree

5 files changed

+41
-25
lines changed

5 files changed

+41
-25
lines changed

spring-context/src/main/java/org/springframework/context/config/PropertyPlaceholderBeanDefinitionParser.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser {
3535

36-
private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode";
36+
private static final String SYSTEM_PROPERTIES_MODE_ATTRIBUTE = "system-properties-mode";
3737

3838
private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";
3939

@@ -43,13 +43,13 @@ protected Class<?> getBeanClass(Element element) {
4343
// As of Spring 3.1, the default value of system-properties-mode has changed from
4444
// 'FALLBACK' to 'ENVIRONMENT'. This latter value indicates that resolution of
4545
// placeholders against system properties is a function of the Environment and
46-
// its current set of PropertySources
47-
if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
46+
// its current set of PropertySources.
47+
if (SYSTEM_PROPERTIES_MODE_DEFAULT.equals(element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE))) {
4848
return PropertySourcesPlaceholderConfigurer.class;
4949
}
5050

51-
// the user has explicitly specified a value for system-properties-mode. Revert
52-
// to PropertyPlaceholderConfigurer to ensure backward compatibility.
51+
// The user has explicitly specified a value for system-properties-mode: revert to
52+
// PropertyPlaceholderConfigurer to ensure backward compatibility with 3.0 and earlier.
5353
return PropertyPlaceholderConfigurer.class;
5454
}
5555

@@ -60,12 +60,16 @@ protected void doParse(Element element, BeanDefinitionBuilder builder) {
6060
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
6161
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
6262

63-
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB);
63+
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE);
6464
if (StringUtils.hasLength(systemPropertiesModeName) &&
6565
!systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
6666
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_" + systemPropertiesModeName);
6767
}
6868

69+
if (element.hasAttribute("value-separator")) {
70+
builder.addPropertyValue("valueSeparator", element.getAttribute("value-separator"));
71+
}
72+
6973
if (element.hasAttribute("null-value")) {
7074
builder.addPropertyValue("nullValue", element.getAttribute("null-value"));
7175
}

spring-context/src/main/resources/org/springframework/context/config/spring-context-4.2.xsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@
144144
</xsd:restriction>
145145
</xsd:simpleType>
146146
</xsd:attribute>
147+
<xsd:attribute name="value-separator" default=":">
148+
<xsd:annotation>
149+
<xsd:documentation><![CDATA[
150+
The separating character between the placeholder variable and the associated
151+
default value: by default, a ':' symbol.
152+
]]></xsd:documentation>
153+
</xsd:annotation>
154+
</xsd:attribute>
147155
<xsd:attribute name="null-value">
148156
<xsd:annotation>
149157
<xsd:documentation><![CDATA[

spring-context/src/test/java/org/springframework/context/config/ContextNamespaceHandlerTests.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Arjen Poutsma
3939
* @author Dave Syer
4040
* @author Chris Beams
41+
* @author Juergen Hoeller
4142
* @since 2.5.6
4243
*/
4344
public class ContextNamespaceHandlerTests {
@@ -55,8 +56,7 @@ public void propertyPlaceholder() throws Exception {
5556
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
5657
.getBeansOfType(PlaceholderConfigurerSupport.class);
5758
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
58-
String s = (String) applicationContext.getBean("string");
59-
assertEquals("bar", s);
59+
assertEquals("bar", applicationContext.getBean("string"));
6060
assertEquals("null", applicationContext.getBean("nullString"));
6161
}
6262

@@ -69,11 +69,11 @@ public void propertyPlaceholderSystemProperties() throws Exception {
6969
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
7070
.getBeansOfType(PropertyPlaceholderConfigurer.class);
7171
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
72-
String s = (String) applicationContext.getBean("string");
73-
assertEquals("spam", s);
72+
assertEquals("spam", applicationContext.getBean("string"));
73+
assertEquals("none", applicationContext.getBean("fallback"));
7474
}
7575
finally {
76-
if (value!=null) {
76+
if (value != null) {
7777
System.setProperty("foo", value);
7878
}
7979
}
@@ -89,8 +89,8 @@ public void propertyPlaceholderEnvironmentProperties() throws Exception {
8989
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
9090
.getBeansOfType(PlaceholderConfigurerSupport.class);
9191
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
92-
String s = (String) applicationContext.getBean("string");
93-
assertEquals("spam", s);
92+
assertEquals("spam", applicationContext.getBean("string"));
93+
assertEquals("none", applicationContext.getBean("fallback"));
9494
}
9595

9696
@Test
@@ -100,12 +100,9 @@ public void propertyPlaceholderLocation() throws Exception {
100100
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
101101
.getBeansOfType(PropertyPlaceholderConfigurer.class);
102102
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
103-
String s = (String) applicationContext.getBean("foo");
104-
assertEquals("bar", s);
105-
s = (String) applicationContext.getBean("bar");
106-
assertEquals("foo", s);
107-
s = (String) applicationContext.getBean("spam");
108-
assertEquals("maps", s);
103+
assertEquals("bar", applicationContext.getBean("foo"));
104+
assertEquals("foo", applicationContext.getBean("bar"));
105+
assertEquals("maps", applicationContext.getBean("spam"));
109106
}
110107

111108
@Test
@@ -115,8 +112,7 @@ public void propertyPlaceholderIgnored() throws Exception {
115112
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
116113
.getBeansOfType(PlaceholderConfigurerSupport.class);
117114
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
118-
String s = (String) applicationContext.getBean("string");
119-
assertEquals("${bar}", s);
115+
assertEquals("${bar}", applicationContext.getBean("string"));
120116
assertEquals("null", applicationContext.getBean("nullString"));
121117
}
122118

spring-context/src/test/resources/org/springframework/context/config/contextNamespaceHandlerTests-simple.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
<constructor-arg value="${foo}"/>
1212
</bean>
1313

14+
<bean id="fallback" class="java.lang.String">
15+
<constructor-arg value="${bar:none}"/>
16+
</bean>
17+
1418
</beans>

spring-context/src/test/resources/org/springframework/context/config/contextNamespaceHandlerTests-system.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
44
xmlns:util="http://www.springframework.org/schema/util"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
7-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
7+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
88

99
<util:properties id="placeholderProps">
1010
<prop key="foo">bar</prop>
1111
</util:properties>
1212

13-
<context:property-placeholder properties-ref="placeholderProps" system-properties-mode="OVERRIDE"/>
13+
<context:property-placeholder properties-ref="placeholderProps" system-properties-mode="OVERRIDE" value-separator="?"/>
1414

1515
<bean id="string" class="java.lang.String">
1616
<constructor-arg value="${foo}"/>
1717
</bean>
1818

19+
<bean id="fallback" class="java.lang.String">
20+
<constructor-arg value="${bar?none}"/>
21+
</bean>
22+
1923
</beans>

0 commit comments

Comments
 (0)