Documentation

You are viewing the documentation for the 2.3.x release series. The latest stable release series is 2.4.x.

§Dependency Injection

Play does not use any dependency injection framework under the hood, and leaves the choice of DI framework (if any) in your hands. Models, Services and Configuration objects can all be handled transparently, and do not need explicit configuration to work in Play.

There is one case which requires explicit configuration, which involves how controllers (which are singleton objects by default) interact with routes.

§Controller Injection

In Play, routes which start with @ are managed by play.api.GlobalSettings#getControllerInstance,

Given the following route definition:

GET     /                  @controllers.SomeController.index()

Then you can manage controller class instantiation using a DI framework by overriding getControllerInstance in your application’s Global class:

object Global extends play.api.GlobalSettings {

  private val injector = SomeDependencyInjectionFramework

  override def getControllerInstance[A](controllerClass: Class[A]): A = {
    injector.getInstance(controllerClass)
  }
}

§Example Projects

The pace of development and the myriad of options even within a single DI framework means that full documentation is beyond the scope of this documentation. However, there are a number of sample projects that demonstrate how to best leverage DI in your project.

§Activator

Typesafe Activator is a local web & command-line tool that helps developers get started with the Typesafe Platform. Using Activator is highly recommended, as you can download a number of sample projects at once and walk through tutorials presented through the Activator UI.

§Spring

Spring is a popular application framework for Java that has dependency injection at its core. There is also an additional project Spring Scala, which provides additional integration options using Scala and Spring.

There is an Activator project available for Spring. You can download it from Activator directly, or clone it from https://github.com/typesafehub/play-spring-data-jpa.

§Subcut

Subcut is a lightweight dependency injection framework written for Scala that uses implicits to pass configuration through injectable classes.

There is a Github project by the Subcut team that shows how to integrate Subcut with Play. You can clone it from https://github.com/dickwall/play-subcut and it is also an Activator project.

§Macwire

Macwire is a lightweight dependency injection framework that uses Scala macros.

There is an Activator project available for Macwire. You can download it from Activator directly, or clone it from https://github.com/adamw/macwire-activator.

§Guice

Guice is a lightweight dependency injection framework designed for Java.

There is an Activator project available for Guice. You can download it from Activator directly, or clone it from https://github.com/typesafehub/play-guice.

§Scaldi

Scaldi is a lightweight Scala dependency injection library.

There is an Activator template available for Scaldi + Play example application. You can download it from Activator directly, or clone it from https://github.com/scaldi/scaldi-play-example. This article can also help you to make your first steps with Scaldi and Play.


Dokümantasyonun bu çevirisi Play ekibi tarafından yapılmamaktadır. Eğer bir hata bulduysanız, bu sayfanın kaynak kodu burada bulunmaktadır. Dokümantasyon yönergelerini okuduktan sonra lütfen katkı yapmaktan çekinmeyin.