Last active
May 11, 2018 14:52
-
-
Save harrylaou/7d95e90cef64522fe98e81673d9f0f62 to your computer and use it in GitHub Desktop.
Compile time DI for play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package application | |
import akka.actor.Scheduler | |
import application.filters.ExampleFilter | |
import com.typesafe.config.Config | |
import controllers.HomeController | |
import play.api.ApplicationLoader.Context | |
import play.api.BuiltInComponentsFromContext | |
import play.api.cache.EhCacheComponents | |
import router.Routes | |
import scala.concurrent.ExecutionContext | |
/** | |
* | |
*/ | |
class AppComponents(context: Context)(implicit val ec: ExecutionContext) | |
extends BuiltInComponentsFromContext(context) | |
with EhCacheComponents { | |
val config: Config = context.initialConfiguration.underlying | |
implicit val scheduler: Scheduler = actorSystem.scheduler | |
lazy val homeController = new HomeController() | |
// order matters - should be the same as routes file | |
lazy val router = new Routes( | |
httpErrorHandler, | |
homeController | |
) | |
val exampleFilter: ExampleFilter = new ExampleFilter() | |
override lazy val httpFilters = Seq(exampleFilter) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.application.loader = application.AppLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package application | |
import play.api.ApplicationLoader.Context | |
import play.api.{Application, ApplicationLoader, LoggerConfigurator} | |
import scala.concurrent.ExecutionContext | |
class AppLoader extends ApplicationLoader { | |
implicit val ec: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext | |
override def load(context: Context): Application = { | |
LoggerConfigurator(context.environment.classLoader).foreach { | |
_.configure(context.environment) | |
} | |
val components: AppComponents = new AppComponents(context) | |
components.application | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api.mvc._ | |
import services.DummyService | |
import scala.concurrent.ExecutionContext | |
class HomeController(implicit ec: ExecutionContext) extends Controller { | |
def index = Action.async { | |
DummyService.doSomething.map(Ok(_)) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be complete you might want to add the configuration in application.conf