Scala, Spring, Maven example

This is a quick, bare minimum example of how Scala, Spring and Maven live happily together.

First, in the default directory structure that Maven offers out of the box, we need to create two new dirs:

  • /src/main/scala
  • /test/main/scala

If you are managing the project from within Eclipse or IntelliJ you’ll have to denote these dirs as source folders explicitly.

The POM looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ScalaSpringMaven</groupId>
    <artifactId>ScalaSpringMaven</artifactId>
    <version>1.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin></plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>

</project>
 

Note how the Scala compiler plugin hijacks the compile and testCompile phases of Maven lifecycle and happily co-exists with the Java compiler.

Next, the application context looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="com.dimitrisli.scala"/>

</beans>

The above Spring config file looks identical like in any other Java project where we want to activate component scanning for post Spring 3 annotation-enabled beans.
Our Bean looks like:

package com.dimitrisli.scala

import org.springframework.stereotype.Component

@Component
class MyBean {

  def performTask = "Greetings from MyBean!"
}

and the Scala Object where we startup the Spring context:

package com.dimitrisli.scala

import org.springframework.context.ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext


object Application {

  def main(args: Array[String]) {

    val context:ApplicationContext = new ClassPathXmlApplicationContext("application-context.xml")
    val myBean:MyBean = context.getBean("myBean").asInstanceOf[MyBean]
    print(myBean performTask)
  }
}

The code can be found in this Github repository.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s