【问题标题】:Play 2.5 Guice errors when compiling编译时播放2.5 Guice错误
【发布时间】:2017-11-27 06:27:30
【问题描述】:

我目前正在将我的应用从 Play 2.4 迁移到 Play 2.5 到目前为止,这是一个巨大的痛苦。

现在,我正在努力让我的测试通过。

我有一个带有一些注入参数的控制器

class UserLogin @Inject() (
    loginService: UserLoginService,
    authConfig: AuthConfig,
    oAuthConfig: OAuthConfig,
    env: PureEnvironment,
    //stringResources: StringResources,
    messagesApi: MessagesApi
  ) extends BaseController(messagesApi: MessagesApi) {

  //endpoints
}

在测试方面,我将它们定义为注入 Guice

object IdentityManagerModuleMock extends AbstractModule with ScalaModule {
  def configure: Unit = {
    bind[PureEnvironment].toInstance(MockEnvironment)
    val conf = Configuration.reference
    val messagesApi = new DefaultMessagesApi(Environment.simple(), conf, new DefaultLangs(conf))
    bind[MessagesApi].toInstance(messagesApi)

    bind[AuthConfig].toInstance(
      AuthConfig(
        "https://localhost",
        30,
        3600,
        86400,
        Seq(InetAddress.getByName("127.0.0.1")),
        Seq(InetAddress.getByName("127.0.0.1")),
        "https://localhost/login"
      )
    )
    bind[OAuthConfig].toInstance(oAuthConfigMock)

    bind[UserLoginService].to[UserLoginServiceImpl]
}

val injector = Guice.createInjector(IdentityManagerModuleMock)

When I enable the routes in my routes file, 
POST    /login com.dummy.im.controllers.UserLogin.login
POST    /reset com.dummy.im.controllers.UserLogin.reset

1) No implementation for com.dummy.im.components.UserLoginService was bound.
[info]   while locating com.dummy.im.components.UserLoginService
[info]     for parameter 0 at com.dummy.im.controllers.UserLogin.<init>(UserLogin.scala:24)
2) No implementation for com.dummy.platform.PureEnvironment was bound.
[info]   while locating com.dummy.platform.PureEnvironment
[info]     for parameter 3 at com.dummy.im.controllers.UserLogin.<init>(UserLogin.scala:24)
3) No implementation for scala.collection.Seq<java.net.InetAddress> was bound.
[info]   while locating scala.collection.Seq<java.net.InetAddress>
[info]     for parameter 4 at com.dummy.im.config.AuthConfig.<init>(AuthConfig.scala:13)

5) Could not find a suitable constructor in java.lang.Integer. Classes must have either one 
(and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
[info]   at java.lang.Integer.class(Integer.java:52)
[info]   while locating java.lang.Integer
[info]     for parameter 1 at com.dummy.im.config.AuthConfig.<init>(AuthConfig.scala:13)

奇怪的是,我有一个非常相似的配置来实际运行应用程序,没有 MessagesAPI(据我所知由 Play 注入)和那些从 .conf 文件中读取的配置对象。

object IdentityManagerModule extends AbstractModule with ScalaModule {

  def configure: Unit = {
    bind[PureEnvironment].to[PlayProductionEnvironmentImpl]

    bind[OauthRepository].to[OauthRepositoryImpl]

    bind[UserLoginService].to[UserLoginServiceImpl]
  }
}

它运行得很好。

我更改的主要内容是在控制器中添加对 MessagesAPI 的依赖项。

我不明白为什么 Guice 看不到 IdentityManagerModuleMock 中绑定的东西。

欢迎提出任何想法。在过去的几天里,我已经阅读并尝试了所有我能想到的。

编辑: 我们有一个自定义应用加载器

class CustomApplicationLoader extends GuiceApplicationLoader() {
  //config validation

  override def builder(context: ApplicationLoader.Context): GuiceApplicationBuilder = {
    val conf = context.initialConfiguration
    initialBuilder
      .in(context.environment)
      .loadConfig(conf)
      .overrides(overrides(context): _*)
      .bindings(
        MiscModule,
        CommonConfigurationModule,
        IdentityManagerConfigModule,
        IdentityManagerModule,
        ApplicationLifecycleModule
      )
  }
}

在.conf文件中,用作

play.application.loader = "com.dummy.guice.CustomApplicationLoader"

【问题讨论】:

    标签: scala guice playframework-2.5


    【解决方案1】:

    看起来您不需要自定义应用程序加载器来完成您的工作。如果您禁用开箱即用的 MessagesApi,然后通过 application.conf 添加您自己的模块,这应该意味着您不必覆盖 MessagesApi。

    https://www.playframework.com/documentation/2.5.x/ScalaPlayModules#Registration-Overrides

    如果您正在运行涉及 Guice 的测试,您可以使用 WithApplication

    https://www.playframework.com/documentation/2.5.x/ScalaFunctionalTestingWithSpecs2#WithApplication

    您永远不必直接调用 Guice.createInjector,因为 WithApplication 会调用 GuiceApplicationBuilder:

    https://www.playframework.com/documentation/2.5.x/ScalaTestingWithGuice#GuiceApplicationBuilder

    查看示例项目:它们都有“2.5.x”分支,并且大多数都集成了测试,并将它们作为指导:

    https://github.com/playframework?utf8=%E2%9C%93&q=examples&type=&language=

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多