我正在将 Play 2.3.x 应用程序迁移到 Play 2.5.x,并且在使用依赖注入时遇到了一些问题。
在 2.3 中,我有一个特征HasRemoteActor
,控制器将混合其中以根据配置引用某个远程参与者。由于这需要应用程序的配置对象,因此现在需要将其变成一个类,以便可以注入配置。这是我的尝试:
/*
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) {
// ...
}
但是,当我启动我的应用程序时,我发现无论端口号如何,actor 系统总是无法找到它的端口(即使没有在该端口上监听)。
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
注入的时间似乎有问题,但我对 DI 太陌生了,我在调试它时遇到了麻烦。
我尝试向我的注入路由的关联控制器添加routesGenerator := InjectedRoutesGenerator
前缀build.sbt
,@
但仍然发现相同的运行时异常。
有人有建议吗?