Skip to content

Commit fbce184

Browse files
committed
Context namespace exposes null-value attribute for property-placeholder element
Issue: SPR-13461
1 parent b23c232 commit fbce184

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,8 +37,8 @@ protected Class<?> getBeanClass(Element element) {
3737

3838
@Override
3939
protected void doParse(Element element, BeanDefinitionBuilder builder) {
40-
4140
super.doParse(element, builder);
41+
4242
builder.addPropertyValue("ignoreInvalidKeys",
4343
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
4444

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,8 +34,10 @@
3434
class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser {
3535

3636
private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode";
37+
3738
private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";
3839

40+
3941
@Override
4042
protected Class<?> getBeanClass(Element element) {
4143
// As of Spring 3.1, the default value of system-properties-mode has changed from
@@ -61,7 +63,11 @@ protected void doParse(Element element, BeanDefinitionBuilder builder) {
6163
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB);
6264
if (StringUtils.hasLength(systemPropertiesModeName) &&
6365
!systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
64-
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_"+systemPropertiesModeName);
66+
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_" + systemPropertiesModeName);
67+
}
68+
69+
if (element.hasAttribute("null-value")) {
70+
builder.addPropertyValue("nullValue", element.getAttribute("null-value"));
6571
}
6672
}
6773

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
]]></xsd:documentation>
2020
</xsd:annotation>
2121

22-
<xsd:complexType name="propertyPlaceholder">
22+
<xsd:complexType name="propertyLoading">
2323
<xsd:attribute name="location" type="xsd:string">
2424
<xsd:annotation>
2525
<xsd:documentation><![CDATA[
@@ -112,7 +112,7 @@
112112
</xsd:annotation>
113113
<xsd:complexType>
114114
<xsd:complexContent>
115-
<xsd:extension base="propertyPlaceholder">
115+
<xsd:extension base="propertyLoading">
116116
<xsd:attribute name="system-properties-mode" default="ENVIRONMENT">
117117
<xsd:annotation>
118118
<xsd:documentation><![CDATA[
@@ -144,6 +144,14 @@
144144
</xsd:restriction>
145145
</xsd:simpleType>
146146
</xsd:attribute>
147+
<xsd:attribute name="null-value">
148+
<xsd:annotation>
149+
<xsd:documentation><![CDATA[
150+
A value that should be treated as {@code null} when resolved as a placeholder value:
151+
e.g. "" (empty String) or "null". By default, no such null value is defined.
152+
]]></xsd:documentation>
153+
</xsd:annotation>
154+
</xsd:attribute>
147155
</xsd:extension>
148156
</xsd:complexContent>
149157
</xsd:complexType>
@@ -163,7 +171,7 @@
163171
</xsd:annotation>
164172
<xsd:complexType>
165173
<xsd:complexContent>
166-
<xsd:extension base="propertyPlaceholder"/>
174+
<xsd:extension base="propertyLoading"/>
167175
</xsd:complexContent>
168176
</xsd:complexType>
169177
</xsd:element>

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@ public void tearDown() {
4747
System.getProperties().remove("foo");
4848
}
4949

50+
5051
@Test
5152
public void propertyPlaceholder() throws Exception {
5253
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
@@ -55,7 +56,8 @@ public void propertyPlaceholder() throws Exception {
5556
.getBeansOfType(PlaceholderConfigurerSupport.class);
5657
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
5758
String s = (String) applicationContext.getBean("string");
58-
assertEquals("No properties replaced", "bar", s);
59+
assertEquals("bar", s);
60+
assertEquals("null", applicationContext.getBean("nullString"));
5961
}
6062

6163
@Test
@@ -68,8 +70,9 @@ public void propertyPlaceholderSystemProperties() throws Exception {
6870
.getBeansOfType(PropertyPlaceholderConfigurer.class);
6971
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
7072
String s = (String) applicationContext.getBean("string");
71-
assertEquals("No properties replaced", "spam", s);
72-
} finally {
73+
assertEquals("spam", s);
74+
}
75+
finally {
7376
if (value!=null) {
7477
System.setProperty("foo", value);
7578
}
@@ -87,7 +90,7 @@ public void propertyPlaceholderEnvironmentProperties() throws Exception {
8790
.getBeansOfType(PlaceholderConfigurerSupport.class);
8891
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
8992
String s = (String) applicationContext.getBean("string");
90-
assertEquals("No properties replaced", "spam", s);
93+
assertEquals("spam", s);
9194
}
9295

9396
@Test
@@ -98,11 +101,11 @@ public void propertyPlaceholderLocation() throws Exception {
98101
.getBeansOfType(PropertyPlaceholderConfigurer.class);
99102
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
100103
String s = (String) applicationContext.getBean("foo");
101-
assertEquals("No properties replaced", "bar", s);
104+
assertEquals("bar", s);
102105
s = (String) applicationContext.getBean("bar");
103-
assertEquals("No properties replaced", "foo", s);
106+
assertEquals("foo", s);
104107
s = (String) applicationContext.getBean("spam");
105-
assertEquals("No properties replaced", "maps", s);
108+
assertEquals("maps", s);
106109
}
107110

108111
@Test
@@ -113,7 +116,8 @@ public void propertyPlaceholderIgnored() throws Exception {
113116
.getBeansOfType(PlaceholderConfigurerSupport.class);
114117
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
115118
String s = (String) applicationContext.getBean("string");
116-
assertEquals("Properties replaced", "${bar}", s);
119+
assertEquals("${bar}", s);
120+
assertEquals("null", applicationContext.getBean("nullString"));
117121
}
118122

119123
@Test
@@ -126,6 +130,7 @@ public void propertyOverride() throws Exception {
126130
Date date = (Date) applicationContext.getBean("date");
127131
Calendar calendar = Calendar.getInstance();
128132
calendar.setTime(date);
129-
assertEquals("No properties overriden", 42, calendar.get(Calendar.MINUTE));
133+
assertEquals(42, calendar.get(Calendar.MINUTE));
130134
}
135+
131136
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:context="http://www.springframework.org/schema/context"
4-
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
5-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
6-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
4+
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-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">
78

89
<util:properties id="placeholderProps">
910
<prop key="foo">bar</prop>
11+
<prop key="baz"></prop>
1012
</util:properties>
1113

12-
<context:property-placeholder properties-ref="placeholderProps" ignore-unresolvable="true"/>
14+
<context:property-placeholder properties-ref="placeholderProps" ignore-unresolvable="true" null-value=""/>
1315

1416
<bean id="string" class="java.lang.String">
1517
<constructor-arg value="${bar}"/>
1618
</bean>
1719

20+
<bean id="nullString" class="java.lang.String" factory-method="valueOf">
21+
<constructor-arg type="java.lang.Object" value="${baz}"/>
22+
</bean>
23+
1824
</beans>
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:context="http://www.springframework.org/schema/context"
4-
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
5-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
6-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
4+
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-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">
78

89
<util:properties id="placeholderProps">
910
<prop key="foo">bar</prop>
11+
<prop key="bar">MYNULL</prop>
1012
</util:properties>
1113

1214
<util:properties id="emptyProps"/>
1315

14-
<context:property-placeholder properties-ref="placeholderProps" order="2"/>
16+
<context:property-placeholder properties-ref="placeholderProps" order="2" null-value="MYNULL"/>
1517

1618
<context:property-placeholder properties-ref="emptyProps" order="1" ignore-unresolvable="true"/>
1719

@@ -21,4 +23,8 @@
2123
<constructor-arg value="${foo}"/>
2224
</bean>
2325

26+
<bean id="nullString" class="java.lang.String" factory-method="valueOf">
27+
<constructor-arg type="java.lang.Object" value="${bar}"/>
28+
</bean>
29+
2430
</beans>

0 commit comments

Comments
 (0)