Specifying the Data Type for Collection Elements

By default, Spring treats every element in a collection as a string. You have to specify the data type for your collection elements if you are not going to use them as strings.

Now suppose you are going to accept a list of integer numbers as the suffixes of your sequence generator. Each number will be formatted into four digits by an instance of java.text. DecimalFormat.

package com.sequence;
public class SequenceGenerator {
private List<Object> suffixes;
public void setSuffixes(List<Object> suffixes) {
this.suffixes = suffixes;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
DecimalFormat formatter = new DecimalFormat("0000");
for (Object suffix : suffixes) {
buffer.append("-");
buffer.append(formatter.format((Integer) suffix));
}
return buffer.toString();
}
}

Then define several suffixes for your sequence generator in the bean configuration file as usual.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="prefixGenerator" ref="datePrefixGenerator" />
<property name="initial" value="100000" />
<property name="suffixes">
<list>
<value>5</value>
<value>10</value>
<value>20</value>
</list>
</property>
</bean>

However, when you run this application, you will encounter a ClassCastException, indicating that the suffixes cannot be cast into integers because their type is String. Spring treats every element in a collection as a string by default. You have to set the type attribute of the <value> tag to specify the element type.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="suffixes">
<list>
<value type="int">5</value>
<value type="int">10</value>
<value type="int">20</value>
</list>
</property>
</bean>

Or you may set the value-type attribute of the collection tag to specify the type for all elements in this collection.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="suffixes">
<list value-type="int">
<value>5</value>
<value>10</value>
<value>20</value>
</list>
</property>
</bean>

In Java 1.5 or higher, you can define your suffixes list with a type-safe collection that stores integers.

package com.sequence;
public class SequenceGenerator {
private List<Integer> suffixes;
public void setSuffixes(List<Integer> suffixes) {
this.suffixes = suffixes;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
DecimalFormat formatter = new DecimalFormat("0000");
for (int suffix : suffixes) {
buffer.append("-");
buffer.append(formatter.format(suffix));
}
return buffer.toString();
}
}

Once you have defined your collections in a type-safe way, Spring will be able to read the collection’s type information through reflection. In this way, you no longer need to specify the value-type attribute of <list>.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator">
<property name="suffixes">
<list>
<value>5</value>
<value>10</value>
<value>20</value>
</list>
</property>
</bean>