在我将应用程序从 2.4 迁移到 2.5(并摆脱所有静态引用)的过程中,我做了以下事情:
class Generic @Inject()(implicit val mat: Materializer, cache: CacheApi, wsClient: WSClient, configuration: play.api.Configuration)
{ ... }
@Singleton
class Concrete1 @Inject() (gw:Generic) { ... }
@Singleton
class Concrete2 @Inject() (gw:Generic) { ... }
要使用它,我会使用 Generic 实例注入 Concrete1/2。它有效,但是在网上看到了其他几个关于它的例子之后,它似乎不太正确。
我正在考虑像这样修改它:
abstract class Generic(cache: CacheApi, wsClient: WSClient, configuration: play.api.Configuration)
{ ... }
@Singleton
class Concrete1(cache: CacheApi, wsClient: WSClient, configuration: play.api.Configuration)
extends Generic(cache, wsClient, configuration) { ... }
@Singleton
class Concrete2(cache: CacheApi, wsClient: WSClient, configuration: play.api.Configuration)
extends Generic(cache, wsClient, configuration) { ... }
然后为了能够做到:@Inject() (c1:Concrete1, c2:Concrete2)
我想我需要它们是由以下定义的模块:https ://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Programmatic-bindings ?在这里做什么更有意义?