Package

play.api

inject

Permalink

package 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.

Source
package.scala
See also

The Module class for information on how to provide bindings.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. inject
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. trait ApplicationLifecycle extends AnyRef

    Permalink

    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())
      }
    
      ...
    }
  2. final case class Binding[T](key: BindingKey[T], target: Option[BindingTarget[T]], scope: Option[Class[_ <: Annotation]], eager: Boolean, source: AnyRef) extends Product with Serializable

    Permalink

    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.

    key

    The binding key.

    target

    The binding target.

    scope

    The JSR-330 scope.

    eager

    Whether the binding should be eagerly instantiated.

    source

    Where this object was bound. Used in error reporting.

    See also

    The Module class for information on how to provide bindings.

  3. final case class BindingKey[T](clazz: Class[T], qualifier: Option[QualifierAnnotation]) extends Product with Serializable

    Permalink

    A binding key.

    A binding key.

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

    clazz

    The class to bind.

    qualifier

    An optional qualifier.

    See also

    The Module class for information on how to provide bindings.

  4. final case class BindingKeyTarget[T](key: BindingKey[_ <: T]) extends BindingTarget[T] with Product with Serializable

    Permalink

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

  5. sealed trait BindingTarget[T] extends AnyRef

    Permalink

    A binding target.

    A binding target.

    This trait captures the four possible types of targets.

    See also

    The Module class for information on how to provide bindings.

  6. class BuiltinModule extends SimpleModule

    Permalink

    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.

  7. class ConfigProvider extends Provider[Config]

    Permalink
  8. class ConfigurationProvider extends Provider[Configuration]

    Permalink
  9. final case class ConstructionTarget[T](implementation: Class[_ <: T]) extends BindingTarget[T] with Product with Serializable

    Permalink

    A binding target that is provided by a class.

    A binding target that is provided by a class.

    See also

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

  10. class DefaultApplicationLifecycle extends ApplicationLifecycle

    Permalink

    Default implementation of the application lifecycle.

    Default implementation of the application lifecycle.

    Annotations
    @Singleton()
  11. trait Injector extends AnyRef

    Permalink

    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.

  12. abstract class Module extends AnyRef

    Permalink

    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]
    )
  13. final case class ProviderConstructionTarget[T](provider: Class[_ <: Provider[_ <: T]]) extends BindingTarget[T] with Product with Serializable

    Permalink

    A binding target that is provided by a provider class.

    A binding target that is provided by a provider class.

    See also

    The Module class for information on how to provide bindings.

  14. final case class ProviderTarget[T](provider: Provider[_ <: T]) extends BindingTarget[T] with Product with Serializable

    Permalink

    A binding target that is provided by a provider instance.

    A binding target that is provided by a provider instance.

    See also

    The Module class for information on how to provide bindings.

  15. sealed trait QualifierAnnotation extends AnyRef

    Permalink

    A qualifier annotation.

    A qualifier annotation.

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

    See also

    The Module class for information on how to provide bindings.

  16. final case class QualifierClass[T <: Annotation](clazz: Class[T]) extends QualifierAnnotation with Product with Serializable

    Permalink

    A qualifier annotation class.

    A qualifier annotation class.

    See also

    The Module class for information on how to provide bindings.

  17. final case class QualifierInstance[T <: Annotation](instance: T) extends QualifierAnnotation with Product with Serializable

    Permalink

    A qualifier annotation instance.

    A qualifier annotation instance.

    See also

    The Module class for information on how to provide bindings.

  18. class RoutesProvider extends Provider[Router]

    Permalink
    Annotations
    @Singleton()
  19. class SimpleInjector extends Injector

    Permalink

    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.

  20. class SimpleModule extends Module

    Permalink

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

Value Members

  1. object BindingKey extends Serializable

    Permalink

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

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

    See also

    The Module class for information on how to provide bindings.

  2. object Modules

    Permalink

    Locates and loads modules from the Play environment.

  3. object NewInstanceInjector extends Injector

    Permalink

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

  4. def bind[T](implicit arg0: ClassTag[T]): BindingKey[T]

    Permalink

    Create a binding key for the given class.

    Create a binding key for the given class.

    See also

    The Module class for information on how to provide bindings.

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

    Permalink

    Create a binding key for the given class.

    Create a binding key for the given class.

    See also

    The Module class for information on how to provide bindings.

  6. package guice

    Permalink

Inherited from AnyRef

Inherited from Any

Ungrouped