title: Scala Dependency Injection in Play Framework
body: <p>I am new to Scala, the PlayFramework, and Dependency Injection. I downloaded the sample scala play framework code. Can someone explain to me why we need to inject the Clock and the appLifeCycle? It is referenced above, so there is no need to inject it right? What is going on here? Why do we need to do this for Web Frameworks in general?</p>
  
  <pre><code>package services
  
  import java.time.{Clock, Instant}
  import javax.inject._
  import play.api.Logger
  import play.api.inject.ApplicationLifecycle
  import scala.concurrent.Future
  
  /**
   * This class demonstrates how to run code when the
   * application starts and stops. It starts a timer when the
   * application starts. When the application stops it prints out how
   * long the application was running for.
   *
   * This class is registered for Guice dependency injection in the
   * [[Module]] class. We want the class to start when the application
   * starts, so it is registered as an "eager singleton". See the code
   * in the [[Module]] class to see how this happens.
   *
   * This class needs to run code when the server stops. It uses the
   * application's [[ApplicationLifecycle]] to register a stop hook.
   */
  @Singleton
  class ApplicationTimer @Inject() (clock: Clock, appLifecycle: ApplicationLifecycle) {
  
    // This code is called when the application starts.
    private val start: Instant = clock.instant
    Logger.info(s"ApplicationTimer demo: Starting application at $start.")
  
    // When the application starts, register a stop hook with the
    // ApplicationLifecycle object. The code inside the stop hook will
    // be run when the application stops.
    appLifecycle.addStopHook { () =&gt;
      val stop: Instant = clock.instant
      val runningTime: Long = stop.getEpochSecond - start.getEpochSecond
      Logger.info(s"ApplicationTimer demo: Stopping application at ${clock.instant} after ${runningTime}s.")
      Future.successful(())
    }
  }
  </code></pre>