我有以下Guice绑定:
val profile = "dev";
bind[DbClient].annotatedWith(Names.named("postgres")).to[PostgresClient].in[Singleton]
我想将profile
作为参数传递给PostgresClient
实例。请介绍如何使用Guice和Scala实现它。
你可以使用@Provides
(在这里描述:https://github.com/google/guice/wiki/ProvidesMethods)
并手动构建您的DbClient
@Provides
@Singleton
@Named("postgres")
def provideDbClient(): DbClient = {
new PostgresClient("dev")
}
我没有尝试过@Singleton
- 但其余的我们经常使用。