【发布时间】:2017-08-30 14:27:34
【问题描述】:
我有一个在初始化客户端时需要隐式 ActorSystem 的外部库 (scala-redis)。我想在我的 Play (2.6) 应用程序中将我的 RedisClient 作为单例,因为将其作为单例是有意义的。
class CustomAppModule(environment: Environment,
configuration: Configuration) extends AbstractModule {
def configure() = {
//val system = getProvider(classOf[ActorSystem]).get()
//val system = ActorSystem()
//bind(classOf[ActorSystem]).toInstance(system)
val redis = RedisClient(configuration.get[String]("redis.host"))(system)
bind(classOf[RedisClient]).toInstance(redis)
}
}
第一个系统失败是因为“在创建 Injector 之前不能使用 Provider”,第二个系统失败是因为 Play Framework 在应用程序启动时初始化了 ActorSystem 本身,第二个系统失败是因为“绑定到 akka.actor。 ActorSystem 已在 play.api.inject.BuiltinModule 中配置。
那么,使用 Guice/DI 处理这种情况的惯用方式是什么?我是否需要一个以 RedisClient 作为值的包装器 Singleton,以及 ActorSystem 可以注入的位置?
【问题讨论】:
标签: scala dependency-injection playframework guice