title: How do I correctly use DI to inject a Play controller's constructor?
body: <p>I'm migrating a Play 2.3.x app to Play 2.5.x and am having some problems using dependency injection.</p>
  
  <p>In 2.3 I had a trait <code>HasRemoteActor</code> which a controller would mix-in to have a reference to some remote actor based on configuration. Since this required the application's config object, it's now required that this becomes a class so the config can be injected. Here's my attempt:</p>
  
  <pre><code>/*
     Dummy controller that has environment and configuration manually injected.
  */
  class ConfigurationController(env: play.api.Environment,
                                conf: play.api.Configuration) extends Controller {
  
  }
  
  /*
    Dummy controller that has environment and configuration manually injected, but 
    sets up a remote client.
  */ 
  class RemoteActorController(env: play.api.Environment, conf: play.api.Configuration)
    extends ConfigurationController(env, conf) {
  
    protected val remoteActorName = "foo"
    private val remoteActorConf = conf.underlying.getConfig(remoteActorName)
    private val system = ActorSystem("HttpServerSystem", ConfigFactory.load())
  
    private val tcpInfo = remoteActorConf.getConfig("akka.remote.netty.tcp")
    private val hostname = tcpInfo.getString("hostname")
    private val port = tcpInfo.getString("port")
  
    val path = s"akka.tcp://PubSubMember@$hostname:$port/system/receptionist"
  
    private val initialContacts = Set(ActorPath.fromString(path))
  
  
    protected val client = system.actorOf(
      ClusterClient.props(ClusterClientSettings(system).withInitialContacts(
          initialContacts)),
      "ClusterClient"
    )
  }
  
  /*
     Actual controller whose actions correspond to endpoints in `conf/routes`.
  */
  @Singleton
  class BarController @Inject()(env: play.api.Environment,
                                conf: play.api.Configuration) extends
      RemoteActorController(env, conf) {
  
      // ...
  
  }
  </code></pre>
  
  <p>However, when I start my application, I find that the actor system always fails to find to its port (even though nothing is listening on that port) irrespective of the port number.</p>
  
  <pre><code>play.api.UnexpectedException: Unexpected exception[ProvisionException: Unable to provision, see the following errors:
  
  1) Error injecting constructor, org.jboss.netty.channel.ChannelException: Failed to bind to: /127.0.0.1:8888
  </code></pre>
  
  <p>There seems to be a problem with the timing of the injection, but I'm so new to DI that I'm having trouble debugging it.</p>
  
  <p>I tried adding <code>routesGenerator := InjectedRoutesGenerator</code> to my <code>build.sbt</code> and prefixed my injected routes' associated controllers with <code>@</code>, but still find the same runtime exceptions.</p>
  
  <p>Does anyone have suggestions?</p>