Scala Guice 传递参数

Scala Guice pass parameter

我有以下 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 - 但其他的我们经常使用。