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 jakarta.inject.Inject
class SomeDatabase @Inject() (applicationLifecycle: ApplicationLifecycle) {
private val connectionPool = new SomeConnectionPool()
applicationLifecycle.addStopHook { () =>
Future.successful(connectionPool.shutdown())
}
...
}
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
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 jakarta.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
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
- Self type
-
BindingKey.type
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
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 Serializabletrait Producttrait Equalstrait BindingTarget[T]class Objecttrait Matchableclass AnyShow all
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 Objecttrait Matchableclass Any
- Known subtypes
-
class BindingKeyTarget[T]class ConstructionTarget[T]class ProviderConstructionTarget[T]class ProviderTarget[T]
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
Attributes
- Supertypes
Attributes
- Supertypes
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 Serializabletrait Producttrait Equalstrait BindingTarget[T]class Objecttrait Matchableclass AnyShow all
Default implementation of the application lifecycle.
Default implementation of the application lifecycle.
Attributes
- Supertypes
Attributes
- Supertypes
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 Objecttrait Matchableclass Any
- Known subtypes
-
object NewInstanceInjectorclass SimpleInjector
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 Objecttrait Matchableclass Any
- Known subtypes
-
class I18nModuleclass SimpleModuleclass BuiltinModuleclass CookiesModuleclass LegacyCookiesModuleclass AssetsModuleShow all
Locates and loads modules from the Play environment.
Locates and loads modules from the Play environment.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
Modules.type
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
- Self type
-
NewInstanceInjector.type
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 Serializabletrait Producttrait Equalstrait BindingTarget[T]class Objecttrait Matchableclass AnyShow all
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 Serializabletrait Producttrait Equalstrait BindingTarget[T]class Objecttrait Matchableclass AnyShow all
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 Objecttrait Matchableclass Any
- Known subtypes
-
class QualifierClass[T]class QualifierInstance[T]
A qualifier annotation class.
A qualifier annotation class.
Attributes
- See also
-
The Module class for information on how to provide bindings.
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait QualifierAnnotationclass Objecttrait Matchableclass AnyShow all
A qualifier annotation instance.
A qualifier annotation instance.
Attributes
- See also
-
The Module class for information on how to provide bindings.
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait QualifierAnnotationclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
RoutesProvider.type
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
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
- Known subtypes
Value members
Concrete methods
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.
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.