Add value-type attrib to beans->map->entry element
Previously, the <value> subelement of a map <entry> allowed one to specify the type of a specific map entry value. This patch allows a value-type attribute as well, such that instead of the following syntax <entry key="x-message-ttl"> <value type="java.lang.Long">100</value> </entry> <entry key="x-ha-policy" value="all" /> one can now use the more concise form <entry key="x-message-ttl" value="100" value-type="java.lang.Long"/> <entry key="x-ha-policy" value="all"/> The new value-type attribute may be used at the <map> level as well, indicating that all elements are of the same type. Appropriate tests have been added exercising value-type at the <map> and <entry> levels. Issue: SPR-9249
This commit is contained in:
parent
f3bcb6e2e4
commit
183ac0c1ff
|
@ -78,6 +78,7 @@ import org.springframework.util.xml.DomUtils;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
|
* @author Gary Russell
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
* @see ParserContext
|
* @see ParserContext
|
||||||
* @see DefaultBeanDefinitionDocumentReader
|
* @see DefaultBeanDefinitionDocumentReader
|
||||||
|
@ -1302,13 +1303,24 @@ public class BeanDefinitionParserDelegate {
|
||||||
Object value = null;
|
Object value = null;
|
||||||
boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE);
|
boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE);
|
||||||
boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE);
|
boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE);
|
||||||
|
boolean hasValueTypeAttribute = entryEle.hasAttribute(VALUE_TYPE_ATTRIBUTE);
|
||||||
if ((hasValueAttribute && hasValueRefAttribute) ||
|
if ((hasValueAttribute && hasValueRefAttribute) ||
|
||||||
((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
|
((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
|
||||||
error("<entry> element is only allowed to contain either " +
|
error("<entry> element is only allowed to contain either " +
|
||||||
"'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
|
"'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
|
||||||
}
|
}
|
||||||
|
if ((hasValueTypeAttribute && hasValueRefAttribute) ||
|
||||||
|
(hasValueTypeAttribute && !hasValueAttribute) ||
|
||||||
|
(hasValueTypeAttribute && valueEle != null)) {
|
||||||
|
error("<entry> element is only allowed to contain a 'value-type' " +
|
||||||
|
"attribute when it has a 'value' attribute", entryEle);
|
||||||
|
}
|
||||||
if (hasValueAttribute) {
|
if (hasValueAttribute) {
|
||||||
value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), defaultValueType, entryEle);
|
String valueType = entryEle.getAttribute(VALUE_TYPE_ATTRIBUTE);
|
||||||
|
if (!StringUtils.hasText(valueType)) {
|
||||||
|
valueType = defaultValueType;
|
||||||
|
}
|
||||||
|
value = buildTypedStringValueForMap(entryEle.getAttribute(VALUE_ATTRIBUTE), valueType, entryEle);
|
||||||
}
|
}
|
||||||
else if (hasValueRefAttribute) {
|
else if (hasValueRefAttribute) {
|
||||||
String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE);
|
String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE);
|
||||||
|
|
|
@ -1153,6 +1153,14 @@
|
||||||
]]></xsd:documentation>
|
]]></xsd:documentation>
|
||||||
</xsd:annotation>
|
</xsd:annotation>
|
||||||
</xsd:attribute>
|
</xsd:attribute>
|
||||||
|
<xsd:attribute name="value-type" type="xsd:string">
|
||||||
|
<xsd:annotation>
|
||||||
|
<xsd:documentation><![CDATA[
|
||||||
|
A short-cut alternative to a 'type' attribute on a nested
|
||||||
|
"<value type='...' >...</value>" element.
|
||||||
|
]]></xsd:documentation>
|
||||||
|
</xsd:annotation>
|
||||||
|
</xsd:attribute>
|
||||||
</xsd:complexType>
|
</xsd:complexType>
|
||||||
|
|
||||||
<!-- 'props' collection type -->
|
<!-- 'props' collection type -->
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2012 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.
|
||||||
|
@ -196,11 +196,15 @@ public class XmlBeanCollectionTests {
|
||||||
@Test
|
@Test
|
||||||
public void testMapWithLiteralsAndReferences() throws Exception {
|
public void testMapWithLiteralsAndReferences() throws Exception {
|
||||||
HasMap hasMap = (HasMap) this.beanFactory.getBean("mixedMap");
|
HasMap hasMap = (HasMap) this.beanFactory.getBean("mixedMap");
|
||||||
assertTrue(hasMap.getMap().size() == 3);
|
assertTrue(hasMap.getMap().size() == 5);
|
||||||
assertTrue(hasMap.getMap().get("foo").equals(new Integer(10)));
|
assertTrue(hasMap.getMap().get("foo").equals(new Integer(10)));
|
||||||
TestBean jenny = (TestBean) this.beanFactory.getBean("jenny");
|
TestBean jenny = (TestBean) this.beanFactory.getBean("jenny");
|
||||||
assertTrue(hasMap.getMap().get("jenny") == jenny);
|
assertTrue(hasMap.getMap().get("jenny") == jenny);
|
||||||
assertTrue(hasMap.getMap().get(new Integer(5)).equals("david"));
|
assertTrue(hasMap.getMap().get(new Integer(5)).equals("david"));
|
||||||
|
assertTrue(hasMap.getMap().get("bar") instanceof Long);
|
||||||
|
assertTrue(hasMap.getMap().get("bar").equals(new Long(100)));
|
||||||
|
assertTrue(hasMap.getMap().get("baz") instanceof Integer);
|
||||||
|
assertTrue(hasMap.getMap().get("baz").equals(new Integer(200)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
|
||||||
|
|
||||||
<bean id="jenny" class="test.beans.TestBean">
|
<bean id="jenny" class="test.beans.TestBean">
|
||||||
<property name="name"><value>Jenny</value></property>
|
<property name="name"><value>Jenny</value></property>
|
||||||
|
@ -133,7 +133,7 @@
|
||||||
|
|
||||||
<bean id="mixedMap" class="org.springframework.beans.factory.xml.HasMap">
|
<bean id="mixedMap" class="org.springframework.beans.factory.xml.HasMap">
|
||||||
<property name="map">
|
<property name="map">
|
||||||
<map>
|
<map value-type="java.lang.Integer">
|
||||||
<entry key-ref="fooKey">
|
<entry key-ref="fooKey">
|
||||||
<value type="java.lang.Integer">10</value>
|
<value type="java.lang.Integer">10</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
@ -151,6 +151,8 @@
|
||||||
</key>
|
</key>
|
||||||
<idref bean="david"/>
|
<idref bean="david"/>
|
||||||
</entry>
|
</entry>
|
||||||
|
<entry key="bar" value="100" value-type="java.lang.Long"/>
|
||||||
|
<entry key="baz" value="200"/>
|
||||||
</map>
|
</map>
|
||||||
</property>
|
</property>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
Loading…
Reference in New Issue