Coder Social home page Coder Social logo

play20-spring-demo's Introduction

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.*;
import org.springframework.context.support.*;

public class Global extends GlobalSettings {

	private ApplicationContext ctx;

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

	@Override
	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="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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

</beans>

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;

@org.springframework.stereotype.Service
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.*;

@org.springframework.stereotype.Controller
public class Application extends Controller {

	@Autowired
	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.

play20-spring-demo's People

Contributors

guillaumebort avatar

Watchers

Kalman avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.