Scala Play将存储库注入控制器

时间:2018-11-22 14:41:54

标签: postgresql scala playframework slick

我的存储库

import domain.{Db, User, UsersTable}
import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile


class UsersRepository(val config: DatabaseConfig[JdbcProfile])
  extends Db with UsersTable {

  import config.profile.api._

  def insert(user: User) = db.run(users += user)

}

域/用户

import play.api.libs.json.{ Json}

case class User (id:Long ,firstName: String, lastName:String)

object User {
  implicit val writeUser = Json.writes[User]
  implicit val readUser = Json.reads[User]
  implicit val formatUser = Json.format[User]
}

UsersTable

import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile
import services.UserTable

trait Db {
  val config: DatabaseConfig[JdbcProfile]
  val db: JdbcProfile#Backend#Database = config.db
}

trait UsersTable { this: Db =>
  import config.profile.api._
  private class Users(tag: Tag) extends Table[User](tag, "users") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

    def firstName = column[String]("first_name")

    def lastName = column[String]("last_name")
    // Select
    def * = (id, firstName, lastName) <> ((User.apply _).tupled, User.unapply)
  }
  val users = TableQuery[UserTable]
}

让我说我在这里注入该仓库

class UserController @Inject()(repo: UsersRepository, cc: ControllerComponents, parsers: PlayBodyParsers)(implicit exec: ExecutionContext) extends AbstractController(cc) {

我收到此错误

Could not find a suitable constructor in repository.UsersRepository. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.

您可以告诉我,我不是scala程序员,所以可以提供任何帮助 顺便说一句,我一直不喜欢这个article到这个地方。

2 个答案:

答案 0 :(得分:1)

作为例外建议:..constructor annotated with @Inject or a zero-argument constructor that is not private.必须将存储库的构造函数更改为:

class UsersRepository @Inject()(val config: DatabaseConfig[JdbcProfile])
  extends Db with UsersTable {

  ...
}

如果这仍然给出错误,则意味着无法插入DatabaseConfig。然后可以像这样使用UsersRepository

class UsersRepository
  extends Db with UsersTable {

  val config: DatabaseConfig[JdbcProfile] = ??? //actual implementation
}

或提供适合DatabaseConfig[JdbcProfile]

的实现

答案 1 :(得分:0)

如果我尝试注入该配置,这就是我要得到的

CreationException: Unable to create injector, see the following errors:

1) No implementation for slick.basic.DatabaseConfig<slick.jdbc.JdbcProfile> was bound.
  while locating slick.basic.DatabaseConfig<slick.jdbc.JdbcProfile>
    for the 1st parameter of repository.UsersRepository.<init>(UsersRepository.scala:11)
  while locating repository.UsersRepository
    for the 1st parameter of services.UserService.<init>(UserService.scala:31)
  while locating services.UserService
    for the 1st parameter of controllers.UserController.<init>(UserController.scala:15)
  while locating controllers.UserController
    for the 3rd parameter of router.Routes.<init>(Routes.scala:29)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:121):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

1 error