Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Defining Scala Beans in Spring XML

Arjen Poutsma edited this page Oct 24, 2012 · 11 revisions

Defining Scala Beans in Spring XML

The Spring container only supports bean properties that follow the JavaBean contract. Properties on these classes are expressed using Java getters and setters (eg. String getFoo() and void setFoo(String)).

By default, Scala classes do not follow the JavaBeans contract. In order to wire up Scala classes as Spring beans, one has to choose between three different strategies described below.

Constructor Injection

The easiest way to wire up a Scala Bean in a Spring XML application context is to simply use constructor injection.

For example, imagine we have the following Scala class:

class Person(val firstName: String, val lastName: String) {

}

You can wire up this class like so:

<bean id="constructor" class="Person">
    <constructor-arg value="John"/>
    <constructor-arg value="Doe"/>
</bean>

As an extra bonus, you also gain immutability

@BeanProperty

Using Spring Scala

Clone this wiki locally