【发布时间】: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