【问题标题】:Upgrading to Guice 4 in Play app now seeing "No implementation for CLASSNAME was bound."在 Play 应用中升级到 Guice 4 现在会看到“没有绑定 CLASSNAME 的实现”。
【发布时间】:2018-02-22 01:55:42
【问题描述】:

我正在从 Play 2.3.x -> 2.4.x 升级项目,因此必须将 jar 依赖项从 Guice 3.x 更新到 4.x。

我正在使用 Guice 像这样在控制器中注入服务:

import javax.inject._

@Singleton
class HomeController @Inject()(service: MyService) extends Controller

我的 jar 中用于 guice 绑定的 configure() 方法如下所示(此处仅提供一个示例):

bind(classOf[MyService]).in(classOf[Singleton])
bind(classOf[OtherService]).in(classOf[Singleton])

只想重申,这些绑定是在一个单独的 jar 中定义的,它是我的 play 项目的依赖项。使用已在 guice 绑定(MyService 或 OtherService)中定义的服务进行注入工作正常

import com.google.inject.Inject

class MyService @Inject()(otherService: OtherService)

但是,如果我添加任何其他使用 AbstractModule 接口手动构建的依赖项,如下所示:

def configure() {
    val config = getConfig
    bind(classOf[Config]).toInstance(config)
}

并像这样注入MyService

class MyService @Inject()(otherService: OtherService, config: Config) with InitializingBean

在运行时的依赖注入期间,我收到以下错误:

No implementation for Config was bound.

知道为什么 Guice 3.0 -> 4.0 升级会导致这种情况吗?或者我可以在下一步探索的任何想法?感谢您的帮助。

【问题讨论】:

  • 添加了一个答案,请检查它是否能解决您的问题。

标签: dependency-injection playframework guice


【解决方案1】:

首先你导入类

import models.userClass

然后在类声明行中注入您的类/特征,而不是在类的主体中:

class MyClass @Inject()(val config: Configuration, var userClass: UserClass){...}

如果它是控制器类,您应该使用InjectedController 扩展它。

【讨论】:

  • 看起来 InjectedController 在 Play 2.4.x 中还不能作为 trait 使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-10
  • 2018-08-04
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多