play.api.inject

package play.api.inject

Play's runtime dependency injection abstraction.

Play's runtime dependency injection support is built on JSR-330, which provides a specification for declaring how dependencies get wired to components. JSR-330 however does not address how components are provided to or located by a DI container. Play's API seeks to address this in a DI container agnostic way.

The reason for providing this abstraction is so that Play, the modules it provides, and third party modules can all express their bindings in a way that is not specific to any one DI container.

Components are bound in the DI container. Each binding is identified by a BindingKey, which is typically an interface that the component implements, and may be optionally qualified by a JSR-330 qualifier annotation. A binding key is bound to a BindingTarget, which describes how the implementation of the interface that the binding key represents is constructed or provided. Bindings may also be scoped using JSR-330 scope annotations.

Bindings are provided by instances of Module.

Out of the box, Play provides an implementation of this abstraction using Guice.

Attributes

See also

The Module class for information on how to provide bindings.

Members list

Type members

Classlikes

Application lifecycle register.

Application lifecycle register.

This is used to hook into Play lifecycle events, specifically, when Play is stopped. The reason Play only provides lifecycle callbacks for stopping is that constructors are considered the application start callback. This has several advantages:

  • It simplifies implementation, if you want to start something, just do it in the constructor.
  • It simplifies state, there's no transitional state where an object has been created but not started yet. Hence, as long as you have a reference to something, it's safe to use it.
  • It solves startup dependencies in a type safe manner - the order that components must be started is enforced by the order that they must be instantiated due to the component graph.

Stop hooks are executed when the application is shutdown, in reverse from when they were registered. Due to this reverse ordering, a component can know that it is safe to use the components it depends on as long as it hasn't received a shutdown event.

To use this, declare a dependency on ApplicationLifecycle, and then register the stop hook when the component is started. For example:

 import play.api.inject.ApplicationLifecycle
 import javax.inject.Inject

 class SomeDatabase @Inject() (applicationLifecycle: ApplicationLifecycle) {

   private val connectionPool = new SomeConnectionPool()
   applicationLifecycle.addStopHook { () =>
     Future.successful(connectionPool.shutdown())
   }

   ...
 }

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
final case class Binding[T](key: BindingKey[T], target: Option[BindingTarget[T]], scope: Option[Class[_ <: Annotation]], eager: Boolean, source: Object)

A binding.

A binding.

Bindings are used to bind classes, optionally qualified by a JSR-330 qualifier annotation, to instances, providers or implementation classes.

Bindings may also specify a JSR-330 scope. If, and only if that scope is javax.inject.Singleton, then the binding may declare itself to be eagerly instantiated. In which case, it should be eagerly instantiated when Play starts up.

Value parameters

eager

Whether the binding should be eagerly instantiated.

key

The binding key.

scope

The JSR-330 scope.

source

Where this object was bound. Used in error reporting.

target

The binding target.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object BindingKey

Constructor for a binding Key that doesn't have a qualifier.

Constructor for a binding Key that doesn't have a qualifier.

Attributes

See also

The Module class for information on how to provide bindings.

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
BindingKey.type
final case class BindingKey[T](clazz: Class[T], qualifier: Option[QualifierAnnotation])

A binding key.

A binding key.

A binding key consists of a class and zero or more JSR-330 qualifiers.

Value parameters

clazz

The class to bind.

qualifier

An optional qualifier.

Attributes

See also

The Module class for information on how to provide bindings.

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class BindingKeyTarget[T](key: BindingKey[_ <: T]) extends BindingTarget[T]

A binding target that is provided by another key - essentially an alias.

A binding target that is provided by another key - essentially an alias.

Attributes

Supertypes
trait Serializable
trait Product
trait Equals
trait BindingTarget[T]
class Object
trait Matchable
class Any
Show all
sealed trait BindingTarget[T]

A binding target.

A binding target.

This trait captures the four possible types of targets.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
class Object
trait Matchable
class Any
Known subtypes
class BuiltinModule extends SimpleModule

The Play BuiltinModule.

The Play BuiltinModule.

Provides all the core components of a Play application. This is typically automatically enabled by Play for an application.

Attributes

Supertypes
class SimpleModule
class Module
class Object
trait Matchable
class Any
class ConfigProvider(configuration: Configuration) extends Provider[Config]

Attributes

Supertypes
trait Provider[Config]
class Object
trait Matchable
class Any

Attributes

Supertypes
class Object
trait Matchable
class Any
final case class ConstructionTarget[T](implementation: Class[_ <: T]) extends BindingTarget[T]

A binding target that is provided by a class.

A binding target that is provided by a class.

Attributes

See also

The play.api.inject.Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
trait BindingTarget[T]
class Object
trait Matchable
class Any
Show all

Default implementation of the application lifecycle.

Default implementation of the application lifecycle.

Attributes

Supertypes
class Object
trait Matchable
class Any

Attributes

Supertypes
class Object
trait Matchable
class Any
trait Injector

An injector, capable of providing components.

An injector, capable of providing components.

This is an abstraction over whatever dependency injection is being used in Play. A minimal implementation may only call newInstance on the passed in class.

This abstraction is primarily provided for libraries that want to remain agnostic to the type of dependency injection being used. End users are encouraged to use the facilities provided by the dependency injection framework they are using directly, for example, if using Guice, use com.google.inject.Injector instead of this.

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
abstract class Module

A Play dependency injection module.

A Play dependency injection module.

Dependency injection modules can be used by Play plugins to provide bindings for JSR-330 compliant ApplicationLoaders. Any plugin that wants to provide components that a Play application can use may implement one of these.

Providing custom modules can be done by appending their fully qualified class names to play.modules.enabled in application.conf, for example

 play.modules.enabled += "com.example.FooModule"
 play.modules.enabled += "com.example.BarModule"

It is strongly advised that in addition to providing a module for JSR-330 DI, that plugins also provide a Scala trait that constructs the modules manually. This allows for use of the module without needing a runtime dependency injection provider.

The bind methods are provided only as a DSL for specifying bindings. For example:

 def bindings(env: Environment, conf: Configuration) = Seq(
   bind[Foo].to[FooImpl],
   bind[Bar].to(new Bar()),
   bind[Foo].qualifiedWith[SomeQualifier].to[OtherFoo]
 )

Attributes

Supertypes
class Object
trait Matchable
class Any
Known subtypes
object Modules

Locates and loads modules from the Play environment.

Locates and loads modules from the Play environment.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Modules.type
object NewInstanceInjector extends Injector

An injector that simply creates a new instance of the passed in classes using the classes no-arg constructor.

An injector that simply creates a new instance of the passed in classes using the classes no-arg constructor.

Attributes

Supertypes
trait Injector
class Object
trait Matchable
class Any
Self type
final case class ProviderConstructionTarget[T](provider: Class[_ <: Provider[_ <: T]]) extends BindingTarget[T]

A binding target that is provided by a provider class.

A binding target that is provided by a provider class.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
trait BindingTarget[T]
class Object
trait Matchable
class Any
Show all
final case class ProviderTarget[T](provider: Provider[_ <: T]) extends BindingTarget[T]

A binding target that is provided by a provider instance.

A binding target that is provided by a provider instance.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
trait BindingTarget[T]
class Object
trait Matchable
class Any
Show all
sealed trait QualifierAnnotation

A qualifier annotation.

A qualifier annotation.

Since bindings may specify either annotations, or instances of annotations, this abstraction captures either of those two possibilities.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
class Object
trait Matchable
class Any
Known subtypes
class QualifierClass[T]
final case class QualifierClass[T <: Annotation](clazz: Class[T]) extends QualifierAnnotation

A qualifier annotation class.

A qualifier annotation class.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
final case class QualifierInstance[T <: Annotation](instance: T) extends QualifierAnnotation

A qualifier annotation instance.

A qualifier annotation instance.

Attributes

See also

The Module class for information on how to provide bindings.

Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
class RoutesProvider(injector: Injector, environment: Environment, configuration: Configuration, httpConfig: HttpConfiguration) extends Provider[Router]

Attributes

Companion
object
Supertypes
trait Provider[Router]
class Object
trait Matchable
class Any

Attributes

Companion
class
Supertypes
class Object
trait Matchable
class Any
Self type
class SimpleInjector(fallback: Injector, components: Map[Class[_], Any]) extends Injector

A simple map backed injector.

A simple map backed injector.

This injector is intended for use by compile time injected applications in the transitional period between when Play fully supports dependency injection across the whole code base, and when some parts of Play still access core components through Play's global state. Since Play's global state requires that some components are still dynamically looked up from an injector, when using a compile time DI approach, there is typically no way to dynamically look up components, so this provides a simple implementation of the Injector trait to allow components that Play requires to be dynamically looked up.

It is intended to just hold built in Play components, but may be used to add additional components by end users when required.

The injector is an immutable structure, new components can be added using the + convenience method, which returns a new injector with that component included.

Value parameters

components

The components that this injector provides.

fallback

The injector to fallback to if no component can be found.

Attributes

Supertypes
trait Injector
class Object
trait Matchable
class Any
class SimpleModule(bindingsFunc: (Environment, Configuration) => Seq[Binding[_]]) extends Module

A simple Play module, which can be configured by passing a function or a list of bindings.

A simple Play module, which can be configured by passing a function or a list of bindings.

Attributes

Supertypes
class Module
class Object
trait Matchable
class Any
Known subtypes

Value members

Concrete methods

def bind[T](clazz: Class[T]): BindingKey[T]

Create a binding key for the given class.

Create a binding key for the given class.

Attributes

See also

The Module class for information on how to provide bindings.

def bind[T : ClassTag]: BindingKey[T]

Create a binding key for the given class.

Create a binding key for the given class.

Attributes

See also

The Module class for information on how to provide bindings.