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

Issue: SPR-13461
This commit is contained in:
Juergen Hoeller 2015-09-21 20:47:03 +02:00
parent b23c23279b
commit fbce18418a
6 changed files with 56 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,8 +37,8 @@ class PropertyOverrideBeanDefinitionParser extends AbstractPropertyLoadingBeanDe
@Override @Override
protected void doParse(Element element, BeanDefinitionBuilder builder) { protected void doParse(Element element, BeanDefinitionBuilder builder) {
super.doParse(element, builder); super.doParse(element, builder);
builder.addPropertyValue("ignoreInvalidKeys", builder.addPropertyValue("ignoreInvalidKeys",
Boolean.valueOf(element.getAttribute("ignore-unresolvable"))); Boolean.valueOf(element.getAttribute("ignore-unresolvable")));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,8 +34,10 @@ import org.springframework.util.StringUtils;
class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser { class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser {
private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode"; private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode";
private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT"; private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";
@Override @Override
protected Class<?> getBeanClass(Element element) { protected Class<?> getBeanClass(Element element) {
// As of Spring 3.1, the default value of system-properties-mode has changed from // As of Spring 3.1, the default value of system-properties-mode has changed from
@ -61,7 +63,11 @@ class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBea
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB); String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB);
if (StringUtils.hasLength(systemPropertiesModeName) && if (StringUtils.hasLength(systemPropertiesModeName) &&
!systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) { !systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_"+systemPropertiesModeName); builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_" + systemPropertiesModeName);
}
if (element.hasAttribute("null-value")) {
builder.addPropertyValue("nullValue", element.getAttribute("null-value"));
} }
} }

View File

@ -19,7 +19,7 @@
]]></xsd:documentation> ]]></xsd:documentation>
</xsd:annotation> </xsd:annotation>
<xsd:complexType name="propertyPlaceholder"> <xsd:complexType name="propertyLoading">
<xsd:attribute name="location" type="xsd:string"> <xsd:attribute name="location" type="xsd:string">
<xsd:annotation> <xsd:annotation>
<xsd:documentation><![CDATA[ <xsd:documentation><![CDATA[
@ -112,7 +112,7 @@
</xsd:annotation> </xsd:annotation>
<xsd:complexType> <xsd:complexType>
<xsd:complexContent> <xsd:complexContent>
<xsd:extension base="propertyPlaceholder"> <xsd:extension base="propertyLoading">
<xsd:attribute name="system-properties-mode" default="ENVIRONMENT"> <xsd:attribute name="system-properties-mode" default="ENVIRONMENT">
<xsd:annotation> <xsd:annotation>
<xsd:documentation><![CDATA[ <xsd:documentation><![CDATA[
@ -144,6 +144,14 @@
</xsd:restriction> </xsd:restriction>
</xsd:simpleType> </xsd:simpleType>
</xsd:attribute> </xsd:attribute>
<xsd:attribute name="null-value">
<xsd:annotation>
<xsd:documentation><![CDATA[
A value that should be treated as {@code null} when resolved as a placeholder value:
e.g. "" (empty String) or "null". By default, no such null value is defined.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension> </xsd:extension>
</xsd:complexContent> </xsd:complexContent>
</xsd:complexType> </xsd:complexType>
@ -163,7 +171,7 @@
</xsd:annotation> </xsd:annotation>
<xsd:complexType> <xsd:complexType>
<xsd:complexContent> <xsd:complexContent>
<xsd:extension base="propertyPlaceholder"/> <xsd:extension base="propertyLoading"/>
</xsd:complexContent> </xsd:complexContent>
</xsd:complexType> </xsd:complexType>
</xsd:element> </xsd:element>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -47,6 +47,7 @@ public class ContextNamespaceHandlerTests {
System.getProperties().remove("foo"); System.getProperties().remove("foo");
} }
@Test @Test
public void propertyPlaceholder() throws Exception { public void propertyPlaceholder() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
@ -55,7 +56,8 @@ public class ContextNamespaceHandlerTests {
.getBeansOfType(PlaceholderConfigurerSupport.class); .getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string"); String s = (String) applicationContext.getBean("string");
assertEquals("No properties replaced", "bar", s); assertEquals("bar", s);
assertEquals("null", applicationContext.getBean("nullString"));
} }
@Test @Test
@ -68,8 +70,9 @@ public class ContextNamespaceHandlerTests {
.getBeansOfType(PropertyPlaceholderConfigurer.class); .getBeansOfType(PropertyPlaceholderConfigurer.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string"); String s = (String) applicationContext.getBean("string");
assertEquals("No properties replaced", "spam", s); assertEquals("spam", s);
} finally { }
finally {
if (value!=null) { if (value!=null) {
System.setProperty("foo", value); System.setProperty("foo", value);
} }
@ -87,7 +90,7 @@ public class ContextNamespaceHandlerTests {
.getBeansOfType(PlaceholderConfigurerSupport.class); .getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string"); String s = (String) applicationContext.getBean("string");
assertEquals("No properties replaced", "spam", s); assertEquals("spam", s);
} }
@Test @Test
@ -98,11 +101,11 @@ public class ContextNamespaceHandlerTests {
.getBeansOfType(PropertyPlaceholderConfigurer.class); .getBeansOfType(PropertyPlaceholderConfigurer.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("foo"); String s = (String) applicationContext.getBean("foo");
assertEquals("No properties replaced", "bar", s); assertEquals("bar", s);
s = (String) applicationContext.getBean("bar"); s = (String) applicationContext.getBean("bar");
assertEquals("No properties replaced", "foo", s); assertEquals("foo", s);
s = (String) applicationContext.getBean("spam"); s = (String) applicationContext.getBean("spam");
assertEquals("No properties replaced", "maps", s); assertEquals("maps", s);
} }
@Test @Test
@ -113,7 +116,8 @@ public class ContextNamespaceHandlerTests {
.getBeansOfType(PlaceholderConfigurerSupport.class); .getBeansOfType(PlaceholderConfigurerSupport.class);
assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty()); assertFalse("No PropertyPlaceholderConfigurer found", beans.isEmpty());
String s = (String) applicationContext.getBean("string"); String s = (String) applicationContext.getBean("string");
assertEquals("Properties replaced", "${bar}", s); assertEquals("${bar}", s);
assertEquals("null", applicationContext.getBean("nullString"));
} }
@Test @Test
@ -126,6 +130,7 @@ public class ContextNamespaceHandlerTests {
Date date = (Date) applicationContext.getBean("date"); Date date = (Date) applicationContext.getBean("date");
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
assertEquals("No properties overriden", 42, calendar.get(Calendar.MINUTE)); assertEquals(42, calendar.get(Calendar.MINUTE));
} }
} }

View File

@ -1,18 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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: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 xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.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/util http://www.springframework.org/schema/util/spring-util-2.5.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"> <util:properties id="placeholderProps">
<prop key="foo">bar</prop> <prop key="foo">bar</prop>
<prop key="baz"></prop>
</util:properties> </util:properties>
<context:property-placeholder properties-ref="placeholderProps" ignore-unresolvable="true"/> <context:property-placeholder properties-ref="placeholderProps" ignore-unresolvable="true" null-value=""/>
<bean id="string" class="java.lang.String"> <bean id="string" class="java.lang.String">
<constructor-arg value="${bar}"/> <constructor-arg value="${bar}"/>
</bean> </bean>
<bean id="nullString" class="java.lang.String" factory-method="valueOf">
<constructor-arg type="java.lang.Object" value="${baz}"/>
</bean>
</beans> </beans>

View File

@ -1,17 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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: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 xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.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/util http://www.springframework.org/schema/util/spring-util-2.5.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"> <util:properties id="placeholderProps">
<prop key="foo">bar</prop> <prop key="foo">bar</prop>
<prop key="bar">MYNULL</prop>
</util:properties> </util:properties>
<util:properties id="emptyProps"/> <util:properties id="emptyProps"/>
<context:property-placeholder properties-ref="placeholderProps" order="2"/> <context:property-placeholder properties-ref="placeholderProps" order="2" null-value="MYNULL"/>
<context:property-placeholder properties-ref="emptyProps" order="1" ignore-unresolvable="true"/> <context:property-placeholder properties-ref="emptyProps" order="1" ignore-unresolvable="true"/>
@ -21,4 +23,8 @@
<constructor-arg value="${foo}"/> <constructor-arg value="${foo}"/>
</bean> </bean>
<bean id="nullString" class="java.lang.String" factory-method="valueOf">
<constructor-arg type="java.lang.Object" value="${bar}"/>
</bean>
</beans> </beans>