-
Notifications
You must be signed in to change notification settings - Fork 124
Wiring up Scala Collections in Spring XML
#Wiring up Scala Collections in Spring XML
Though you can use Java collections in Scala, it makes more sense to use the richer collections API defined in the Scala Class Library. Not only does this API define a wide variety of collections, it also contains both mutable and immutable variants.
Spring Scala's support for Scala Collections comes in two forms: PropertyEditor
s and an XML namespaces.
Spring uses the concept of PropertyEditor
s to effect the conversion between different types of object.
Spring Scala has its own set of property editors that supports the Scala Collections API.
With these, you can wire up the following collection types:
-
Seq
(both mutable and immutable) -
IndexedSeq
(both mutable and immutable) ResizableArray
-
LinearSeq
(both mutable and immutable) Buffer
-
Set
(both mutable and immutable) -
Map
(both mutable and immutable)
To enable the support for these collections, you will need to add the following bit of XML to your application context:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<bean class="org.springframework.scala.beans.propertyeditors.ScalaEditorRegistrar"/>
</property>
</bean>
With that out of the way, you can start wiring up collections. For example, the following class:
class ScalaCollectionBean(val scalaSeq: Seq[String])
can be wired up as follows:
<bean id="scalaCollection" class="ScalaCollectionBean">
<constructor-arg>
<list>
<value>Foo</value>
<value>Bar</value>
</list>
</constructor-arg>
</bean>
Behind the curtains, the ScalaCollectionEditor
in Spring Scala will convert the <list>
(effectively a java.util.List
) and convert it to a Scala Seq
.