已配置到 play.api.db.DBApi 的绑定,play-slick 的演变和注入器错误

Posted

技术标签:

【中文标题】已配置到 play.api.db.DBApi 的绑定,play-slick 的演变和注入器错误【英文标题】:A binding to play.api.db.DBApi was already configured, evolutions and injector error with play-slick 【发布时间】:2016-01-05 09:32:33 【问题描述】:

我想在我的play项目中引入slick,所以我在build.sbt中添加了以下依赖:

"com.typesafe.play"          %% "play-slick"               % "1.0.1"     withSources(),
  "com.typesafe.play"          %% "play-slick-evolutions"    % "1.0.1"     withSources(),

然后,当我为控制器运行集成规范时,我在 Intellij IDE 和命令行激活器测试中都出现了以下异常。谷歌后我找到了解决办法:https://www.playframework.com/documentation/2.4.x/PlaySlickFAQ#A-binding-to-play.api.db.DBApi-was-already-configured

删除 jdbc 依赖后,使用命令行时通过了集成规范,但是当我在 Intellij IDE 上运行测试时仍然遇到相同的错误,如下错误: 在嵌套套件上调用 run 时遇到异常 - 无法创建注入器,请参阅以下错误:

1) A binding to play.api.db.DBApi was already configured at play.api.db.DBModule.bindings(DBModule.scala:25):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1).
  at play.api.db.slick.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:15):
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

1 error
com.google.inject.CreationException: Unable to create injector, see the following errors:

1) A binding to play.api.db.DBApi was already configured at play.api.db.DBModule.bindings(DBModule.scala:25):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1).
  at play.api.db.slick.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:15):
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:466)
    at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
    at com.google.inject.Guice.createInjector(Guice.java:96)
    at com.google.inject.Guice.createInjector(Guice.java:73)
    at com.google.inject.Guice.createInjector(Guice.java:62)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:126)
    at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:93)
    at play.api.test.FakeApplication.<init>(Fakes.scala:216)
    at org.scalatestplus.play.OneServerPerSuite$class.app(OneServerPerSuite.scala:152)

【问题讨论】:

【参考方案1】:

如果你和我一样,你的 build.sbt 中有这样的东西

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test,
  "mysql" % "mysql-connector-java" % "5.1.37",
  "com.typesafe.play" %% "play-slick" % "1.1.1",
  "com.typesafe.play" %% "play-slick-evolutions" % "1.1.1"
)

删除 jdbc

libraryDependencies ++= Seq(
  //jdbc, //<<---- this one here
  cache,

【讨论】:

其实有官方FAQ描述了这个问题。 同时检查anorm 或其他任何使用数据库的东西:)【参考方案2】:

您可能已经向 jdbc 添加了依赖项,如果您使用 slick 则没有意义。删除它,它会工作。

【讨论】:

以上是关于已配置到 play.api.db.DBApi 的绑定,play-slick 的演变和注入器错误的主要内容,如果未能解决你的问题,请参考以下文章

为什么我的线程组列表中有这么多的绑定器和asynctasks?他们是如何管理的?

Lua 5.3 -- SOL2.0 用户指南

NSTableView 绑定到 NSArrayController

Spring Cloud入门 - 微服务与消息驱动

如何列出要注入 grails 的可用对象?

js 超浓缩 双向绑定