Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Using Spring from within a Play 2.0 application

This is a simple application demonstrating how to integrate a Play 2.0 application components with Spring framework.

Note that the same technique can be applied to any other dependency injection framework.

How does it work?

There is a few places where the Spring binding is done.

First, add the spring dependency

In the project/Build.scala file, add a dependency to spring-context:

import sbt._
import Keys._
import PlayProject._

object ApplicationBuild extends Build {

    val appName         = "play-spring"
    val appVersion      = "1.0-SNAPSHOT"

    val appDependencies = Seq(
      "org.springframework" % "spring-context" % "3.0.7.RELEASE"

    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
      // Add your own project settings here      


Using controllers instances

We wiil use dynamic controller dispatching instead of the statically compiled dispatching used by default in Play framework. To do that, just prefix your controller class name with the @ symbol in the routes file:

GET    /       @controllers.Application.index()

Managing controllers instances

The controllers instances management will be delegated to the Global object of your application. Here is an implementation of the Global using Spring framework:

import play.*;

import org.springframework.context.*;

public class Global extends GlobalSettings {

	private ApplicationContext ctx;

	public void onStart(Application app) {
		ctx = new ClassPathXmlApplicationContext("components.xml");

	public <A> A getControllerInstance(Class<A> clazz) {
		return ctx.getBean(clazz);


And here is the associated conf/components.xml file we are using to configure Spring:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

   <context:component-scan base-package="controllers,services,dao"/>


Creating Spring components

In this example we are using the annotation driven binding for Spring (so we use the annotations provided in org.springframework.stereotype to mark our components as Spring managed).

First a simple "Spring style" service:

package services;

public class HelloService {

	public String hello() {
		return "Hello world!";


And now the controller with the service autowired:

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

import services.*;

import org.springframework.beans.factory.annotation.*;

public class Application extends Controller {

	private HelloService helloService;
  	public Result index() {
    	return ok(index.render(helloService.hello()));

To go further

You can then integrate any Spring componenents to your application. The auto-reload feature will just work magically. If you plan to use a database or an ORM, you could want to manage it directly via Spring instead of using the default Play plugins.


Using Spring from within a Play 2.0 application






No releases published


No packages published