【问题标题】:Scala Play injecting repository into controllerScala Play 将存储库注入控制器
【发布时间】:2018-11-22 14:41:53
【问题描述】:

我的仓库

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]
}

用户表

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]
}

假设我在这里注入了那个 repo

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 去这个地方。

【问题讨论】:

    标签: postgresql scala playframework slick


    【解决方案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]的实现

    【讨论】:

    • 值得注意的是,UsersRepository 也可以转换为 trait 并且 config 只是特征中的一个值,但这意味着您必须使用 @ 将此特征绑定到另一个类987654328@.
    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多