Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Wiring up Scala Collections in Spring XML

Arjen Poutsma edited this page · 4 revisions

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.

The support for Scala Collections in Spring XMLcomes in two forms: PropertyEditors and an XML namespaces. Of course, you can also use Function Bean Configuration to configure your collections in Scala code.

Scala Collection PropertyEditors

Spring uses the concept of PropertyEditors 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.

The scala-util XML Schema

Similar to the util namespace in Spring Java, there is a scala-util namespace in Spring Scala. This namespace contains the seq, set and map elements to define a Seq, Set or Map respectively.

For instance, given the ScalaCollectionBean shown above, you can also wire it up as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:scala-util="http://www.springframework.org/schema/scala/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/scala/util http://www.springframework.org/schema/scala/util/scala-util.xsd">

<bean id="scalaCollection" class="org.springframework.scala.demo.collection.ScalaCollectionBean">
   <constructor-arg ref="scalaSeq"/>
</bean>

<scala-util:seq id="scalaSeq">
   <value>Foo</value>
   <value>Bar</value>
</scala-util:seq>
Something went wrong with that request. Please try again.