Collections
In the
<list/>
, <set/>
, <map/>
, and <props/>
elements, you set the properties and arguments of the Java Collection
types List
, Set
, Map
, andProperties
, respectively.<bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry key="an entry" value="just some string"/> <entry key ="a ref" value-ref="myDataSource"/> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean>
The value of a map key or value, or a set value, can also again be any of the following elements:
bean | ref | idref | list | set | map | props | value | null
Collection merging
The Spring container also supports the merging of collections. An application developer can define a parent-style
<list/>
, <map/>
, <set/>
or <props/>
element, and have child-style <list/>
, <map/>
, <set/>
or <props/>
elements inherit and override values from the parent collection. That is, the child collection’s values are the result of merging the elements of the parent and child collections, with the child’s collection elements overriding values specified in the parent collection.
This section on merging discusses the parent-child bean mechanism. Readers unfamiliar with parent and child bean definitions may wish to read the relevant sectionbefore continuing.
The following example demonstrates collection merging:
<beans> <bean id="parent" abstract="true" class="example.ComplexObject"> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.com</prop> <prop key="support">support@example.com</prop> </props> </property> </bean> <bean id="child" parent="parent"> <property name="adminEmails"> <!-- the merge is specified on the child collection definition --> <props merge="true"> <prop key="sales">sales@example.com</prop> <prop key="support">support@example.co.uk</prop> </props> </property> </bean> <beans>
Notice the use of the
merge=true
attribute on the <props/>
element of the adminEmails
property of the child
bean definition. When the child
bean is resolved and instantiated by the container, the resulting instance has an adminEmails
Properties
collection that contains the result of the merging of the child’s adminEmails
collection with the parent’s adminEmails
collection.administrator=administrator@example.com sales=sales@example.com support=support@example.co.uk
The child
Properties
collection’s value set inherits all property elements from the parent <props/>
, and the child’s value for the support
value overrides the value in the parent collection.
This merging behavior applies similarly to the
<list/>
, <map/>
, and <set/>
collection types. In the specific case of the <list/>
element, the semantics associated with the List
collection type, that is, the notion of an ordered
collection of values, is maintained; the parent’s values precede all of the child list’s values. In the case of the Map
, Set
, and Properties
collection types, no ordering exists. Hence no ordering semantics are in effect for the collection types that underlie the associatedMap
, Set
, and Properties
implementation types that the container uses internally.Limitations of collection merging
You cannot merge different collection types (such as a
Map
and a List
), and if you do attempt to do so an appropriate Exception
is thrown. The merge
attribute must be specified on the lower, inherited, child definition; specifying the merge
attribute on a parent collection definition is redundant and will not result in the desired merging.Strongly-typed collection
With the introduction of generic types in Java 5, you can use strongly typed collections. That is, it is possible to declare a
Collection
type such that it can only containString
elements (for example). If you are using Spring to dependency-inject a strongly-typed Collection
into a bean, you can take advantage of Spring’s type-conversion support such that the elements of your strongly-typed Collection
instances are converted to the appropriate type prior to being added to the Collection
.public class Foo { private Map<String, Float> accounts; public void setAccounts(Map<String, Float> accounts) { this.accounts = accounts; } }
<beans> <bean id="foo" class="x.y.Foo"> <property name="accounts"> <map> <entry key="one" value="9.99"/> <entry key="two" value="2.75"/> <entry key="six" value="3.99"/> </map> </property> </bean> </beans>
When the
accounts
property of the foo
bean is prepared for injection, the generics information about the element type of the strongly-typed Map<String, Float>
is available by reflection. Thus Spring’s type conversion infrastructure recognizes the various value elements as being of type Float
, and the string values 9.99, 2.75
, and 3.99
are converted into an actual Float
type.
No comments:
Post a Comment