Scala Guice传递参数

问题描述 投票:1回答:1

我有以下Guice绑定:

val profile = "dev";
bind[DbClient].annotatedWith(Names.named("postgres")).to[PostgresClient].in[Singleton]

我想将profile作为参数传递给PostgresClient实例。请介绍如何使用Guice和Scala实现它。

scala guice
1个回答
2
投票

你可以使用@Provides(在这里描述:https://github.com/google/guice/wiki/ProvidesMethods

并手动构建您的DbClient

  @Provides
  @Singleton
  @Named("postgres")
  def provideDbClient(): DbClient = {
    new PostgresClient("dev")
  }

我没有尝试过@Singleton - 但其余的我们经常使用。

© www.soinside.com 2019 - 2024. All rights reserved.