未绑定 play.api.db.slick.DatabaseConfigProvider 的实现

No implementation for play.api.db.slick.DatabaseConfigProvider was bound

我无法熟练地使用 play 2。5.x

我收到以下运行时错误:

ProvisionException: Unable to provision, see the following errors:

1) No implementation for play.api.db.slick.DatabaseConfigProvider was bound.
  while locating play.api.db.slick.DatabaseConfigProvider

我的 DAO 看起来像:

@Singleton
class UserDAO @Inject() (protected val dbConfigProvider: DatabaseConfigProvider) 
extends HasDatabaseConfigProvider[JdbcProfile] {
    import driver.api._

...

}

我只是将它注入到我的控制器中,例如:

@Singleton
class UserController @Inject() (ws: WSClient, cache: CacheApi, userDAO: UserDAO) extends Controller {
...
}

build.sbt

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  cache,
  ws,
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
  // database
  jdbc,
  "org.postgresql"     %  "postgresql" % "9.3-1102-jdbc41",
  "com.typesafe.play" %% "play-slick" % "2.0.0"
)

我的 application.conf 有:

play.db {
  # The combination of these two settings results in "db.default" as the
  # default JDBC pool:
  #config = "db"
  #default = "default"

  # Play uses HikariCP as the default connection pool.  You can override
  # settings by changing the prototype:
  prototype {
    # Sets a fixed JDBC connection pool size of 50
    #hikaricp.minimumIdle = 50
    #hikaricp.maximumPoolSize = 50
  }
}

## JDBC Datasource
db {
  default.driver = org.postgresql.Driver
  default.url = "jdbc:postgresql://localhost/testdb_development"
  default.username = "blankman"
  #default.password = ""
}

如果我更改我的数据库名称,我会收到连接错误,因此池会正确获取我的配置设置。

我可以看到您的 application.conf 的一个问题是它缺少 play-slick 特定的配置键。事实上,您应该从 application.conf 中删除 db 部分,并将其替换为 slick.dbs,如 https://www.playframework.com/documentation/2.5.x/PlaySlick#database-configuration

据我所知(基于 Play 2.4.x) 您不能在同一个 Play 项目中同时使用 play-slick 和 jdbc。

我绝对建议您阅读 Play-Slick 文档以更好地了解它的工作原理。

希望对您有所帮助!

我用 application.conf

中的这个配置解决了这个问题
slick.dbs.default.profile="slick.jdbc.PostgresProfile$"
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.url="jdbc:postgresql://127.0.0.1:5432/[db_name]"
slick.dbs.default.db.user=[name_db]
slick.dbs.default.db.password=[pw_bd]

请注意将数据库名称更改为默认值以外的名称。仅更改 db_name.