Mixing Scala and Java in Java Spring

Scala downright rocks at certain things. Java is great in others. How do you mix Scala into an existing Java Spring Maven program without causing trouble? With a few simple tricks that really don’t require much.

Scala uses the JVM and has a matching java collections compatibility. Using these it is possible to update nearly any Java Spring-maven program with Scala classes.

Dependencies for Maven

There is one simple dependency for this to work, the Scala Programming Language. Add the language to your POM as follows.


This will cause the scala language to be included with Maven at runtime.

Wiring your Class with XML

In discovering this, I am using XML due to the need to easily manipulate a class many times. Spring is basically acting as GUI, enforcer, and code reducer.

Class variables are considered as part of the constructor and must include the scala.beans dependency. All variables need the @BeanProperty to work. Furthermore, importing scala.collection.JavaConversions._ will give java collections scala like properties such as the ability to use .foreach().

    import scala.beans._
    import scala.collection.JavaConversions._
    class MyClass{
       var source:String = null
       var fromImports:String = null

This @BeanProperty annotation will create a getter and setter for Spring when the program builds. It is required even when using a constructor argument.

The following xml can be used to wire the bean.

      <property name="source" value="mySource"/>

If the variables are placed in a constructor, wire as such.

  <bean id="Notify" class="com.hygenics.parser.MyClass" parent="imports">
 	<constructor-arg name="source" value="mysource"/>