我有以下 Guice 绑定(bind):
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
- 但其余的我们经常使用。
关于Scala Guice 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55863412/