How do I connect the playback lifecycle to a module instead of a plugin?
You don't specify which language you are using, so I'll cover both quickly. I base both answers on the following repositories:
Java
-
Write your functionality any way you want - there are no concrete classes to extend. If you have dependencies on Play components such as
Configuration
you have to inject them.@Singleton public class MyModuleCode { private final boolean enableWidgets; @javax.inject.Inject public MyModuleCode(final Configuration configuration) { this.enableWidgets = configuration.getBoolean("widgets.enabled", false); } }
Please note that dependency injection is used instead of static link. Also note that I have given this annotation example @Singleton
, but could also have a query scope, for example.
For more information, see the Play DI tabs
-
Open the components of your module. To do this, extend the class
play.api.inject.Module
and implementpublic Seq<Binding<?>> bindings(final Environment environment, final Configuration configuration)
.package com.example.module; public class MyModule extends Module { @Override public Seq<Binding<?>> bindings(final Environment environment, final Configuration configuration) { return seq(bind(MyModuleCode.class).toSelf().in(Singleton.class)); } }
You can also bind implementations to interfaces here, configure instance providers, etc.
-
If this is what you are publicly releasing a module, let's say you are doing it here - that is beyond the scope of the question. Let's also assume that you've added a dependency for a module in the project you're working on.
-
Turn the module on
application.conf
.play { modules { enabled += com.example.module.MyModule } }
-
Components exposed through your module - just
MyModuleCode
in this example - are now available to embed in your controllers, actions, etc. -
If you want a disconnect hook, just insert
ApplicationLifecycle
into the component and register the hook; See https://playframework.com/documentation/2.4.x/JavaDependencyInjection#Stopping/cleaning-up for details .
Scala
-
Write your functionality any way you want - there are no concrete classes to extend. If you have dependencies on Play components such as
CacheApi
you have to inject them.@Singleton class DefaultPatternCache @Inject() (cache: CacheApi) extends PatternCache { override def apply(value: String): Option[Pattern] = cache.getOrElse[Option[Pattern]](key = s"Deadbolt.pattern.$value") { Some(Pattern.compile(value)) } }
Please note that dependency injection is used instead of static link. Also note that I have given this annotation example @Singleton
, but could also have a query scope, for example.
More on in docs for DI playback
-
Open the components of your module. To do this, we extend the class
play.api.inject.Module
and implement itdef bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]]
.package com.example.module import com.example.module.cache.{DefaultPatternCache, PatternCache} import play.api.inject.{Binding, Module} import play.api.{Configuration, Environment} class MyModule extends Module { override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = Seq(bind[PatternCache].to[DefaultPatternCache]) }
Here you can also bind implementations to traits, customize instance providers, etc.
-
If this is what you are publicly releasing a module, let's say you are doing it here - that is beyond the scope of the question. Let's also assume that you've added a dependency for a module in the project you're working on.
-
Turn the module on
application.conf
.play { modules { enabled += com.example.module.MyModule } }
-
Components exposed through your module - just
MyModuleCode
in this example - are now available to embed in your controllers, actions, etc. -
If you want a disconnect hook, just insert
ApplicationLifecycle
into the component and register the hook; See https://playframework.com/documentation/2.4.x/ScalaDependencyInjection#Stopping/cleaning-up for details .
Summary
Modules are nothing special anymore - they are just a way of grouping injectable components.
source to share