Context namespace exposes value-separator attribute for property-placeholder element

Issue: SPR-7794
This commit is contained in:
Juergen Hoeller 2015-09-23 22:39:23 +02:00
parent c7fd4ccf48
commit a23629f60f
5 changed files with 41 additions and 25 deletions

View File

@ -33,7 +33,7 @@ import org.springframework.util.StringUtils;
*/
class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser {
private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode";
private static final String SYSTEM_PROPERTIES_MODE_ATTRIBUTE = "system-properties-mode";
private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";
@ -43,13 +43,13 @@ class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBea
// As of Spring 3.1, the default value of system-properties-mode has changed from
// 'FALLBACK' to 'ENVIRONMENT'. This latter value indicates that resolution of
// placeholders against system properties is a function of the Environment and
// its current set of PropertySources
if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
// its current set of PropertySources.
if (SYSTEM_PROPERTIES_MODE_DEFAULT.equals(element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE))) {
return PropertySourcesPlaceholderConfigurer.class;
}
// the user has explicitly specified a value for system-properties-mode. Revert
// to PropertyPlaceholderConfigurer to ensure backward compatibility.
// The user has explicitly specified a value for system-properties-mode: revert to
// PropertyPlaceholderConfigurer to ensure backward compatibility with 3.0 and earlier.
return PropertyPlaceholderConfigurer.class;
}
@ -60,12 +60,16 @@ class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBea
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB);
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE);
if (StringUtils.hasLength(systemPropertiesModeName) &&
!systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_" + systemPropertiesModeName);
}
if (element.hasAttribute("value-separator")) {
builder.addPropertyValue("valueSeparator", element.getAttribute("value-separator"));
}
if (element.hasAttribute("null-value")) {
builder.addPropertyValue("nullValue", element.getAttribute("null-value"));
}

View File

@ -144,6 +144,14 @@
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="value-separator" default=":">
<xsd:annotation>
<xsd:documentation><![CDATA[
The separating character between the placeholder variable and the associated
default value: by default, a ':' symbol.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="null-value">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@ -38,6 +38,7 @@ import static org.junit.Assert.*;
* @author Arjen Poutsma
* @author Dave Syer
* @author Chris Beams
* @author Juergen Hoeller
* @since 2.5.6
*/
public class ContextNamespaceHandlerTests {
@ -55,8 +56,7 @@ public class ContextNamespaceHandlerTests {
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
.getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string");
assertEquals("bar", s);
assertEquals("bar", applicationContext.getBean("string"));
assertEquals("null", applicationContext.getBean("nullString"));
}
@ -69,8 +69,8 @@ public class ContextNamespaceHandlerTests {
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
.getBeansOfType(PropertyPlaceholderConfigurer.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string");
assertEquals("spam", s);
assertEquals("spam", applicationContext.getBean("string"));
assertEquals("none", applicationContext.getBean("fallback"));
}
finally {
if (value != null) {
@ -89,8 +89,8 @@ public class ContextNamespaceHandlerTests {
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
.getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string");
assertEquals("spam", s);
assertEquals("spam", applicationContext.getBean("string"));
assertEquals("none", applicationContext.getBean("fallback"));
}
@Test
@ -100,12 +100,9 @@ public class ContextNamespaceHandlerTests {
Map<String, PropertyPlaceholderConfigurer> beans = applicationContext
.getBeansOfType(PropertyPlaceholderConfigurer.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("foo");
assertEquals("bar", s);
s = (String) applicationContext.getBean("bar");
assertEquals("foo", s);
s = (String) applicationContext.getBean("spam");
assertEquals("maps", s);
assertEquals("bar", applicationContext.getBean("foo"));
assertEquals("foo", applicationContext.getBean("bar"));
assertEquals("maps", applicationContext.getBean("spam"));
}
@Test
@ -115,8 +112,7 @@ public class ContextNamespaceHandlerTests {
Map<String, PlaceholderConfigurerSupport> beans = applicationContext
.getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string");
assertEquals("${bar}", s);
assertEquals("${bar}", applicationContext.getBean("string"));
assertEquals("null", applicationContext.getBean("nullString"));
}

View File

@ -11,4 +11,8 @@
<constructor-arg value="${foo}"/>
</bean>
<bean id="fallback" class="java.lang.String">
<constructor-arg value="${bar:none}"/>
</bean>
</beans>

View File

@ -2,18 +2,22 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<util:properties id="placeholderProps">
<prop key="foo">bar</prop>
</util:properties>
<context:property-placeholder properties-ref="placeholderProps" system-properties-mode="OVERRIDE"/>
<context:property-placeholder properties-ref="placeholderProps" system-properties-mode="OVERRIDE" value-separator="?"/>
<bean id="string" class="java.lang.String">
<constructor-arg value="${foo}"/>
</bean>
<bean id="fallback" class="java.lang.String">
<constructor-arg value="${bar?none}"/>
</bean>
</beans>