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=""



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=""

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


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

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

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.


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s