Java Reference
In-Depth Information
To define a property of the interface java.util.List in the bean configuration, you specify a <list>
tag that contains the elements. The elements allowed inside the <list> tag can be a simple constant
value specified by <value> , a bean reference by <ref> , an inner bean definition by <bean> , or a null
element by <null> . You can even embed other collections in a collection.
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator">
<property name="prefixGenerator" ref="datePrefixGenerator" />
<property name="initial" value="100000" />
<property name="suffixes">
<list>
<value>A</value>
<bean class="java.net.URL">
<constructor-arg value="http" />
<constructor-arg value=" www.apress.com" />
<constructor-arg value="/" />
</bean>
<null />
</list>
</property>
</bean>
Conceptually, an array is very similar to a list in that it's also an ordered and indexed collection that
can be accessed by index. The main difference is that the length of an array is fixed and cannot be
extended dynamically. In the Java Collections framework, an array and a list can be converted to each
other through the Arrays.asList() and List.toArray() methods. For your sequence generator, you can
use an Object[] array to contain the suffixes and access them either by index or with a for-each loop.
package com.apress.springenterpriserecipes.sequence;
...
public class SequenceGenerator {
...
private Object[] suffixes;
public void setSuffixes( Object[] suffixes) {
this.suffixes = suffixes;
}
...
}
The definition of an array in the bean configuration file is identical to a list denoted by the
<list> tag.
Another common collection type is a set . Both the java.util.List interface and the java.util.Set
interface extend the same interface: java.util.Collection . A set differs from a list in that it is neither
ordered nor indexed, and it can store unique objects only. That means no duplicate element can be
contained in a set. When the same element is added to a set for the second time, it will replace the old
one. The equality of elements is determined by the equals() method.
package com.apress.springenterpriserecipes.sequence;
...
public class SequenceGenerator {
...
private Set<Object> suffixes;
Search WWH ::




Custom Search