在Play应用程序中升级到Guice 4现在看到“没有绑定CLASSNAME的实现。”

时间:2018-02-22 01:55:43

标签: dependency-injection playframework guice

我正在从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升级会导致这个吗?或者我可以探索下一步的任何想法?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

首先导入类

import models.userClass

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

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

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