【问题标题】:Compilation error when using Scaldi使用 Scaldi 时出现编译错误
【发布时间】:2014-12-30 19:40:36
【问题描述】:

我正在按照Scaldi documentaiton 中提到的步骤进行操作。以下是我的代码。

class Game(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean, gamePersistor: GamePersistor) extends Injectable {

  def this(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean)(implicit inj: Injector) {
    this(players, currentPlayer, board, active, inject[GamePersistor])
  }
}

我收到以下编译错误。

Error:(11, 49) not found: value inject
    this(players, currentPlayer, board, active, inject[GamePersistor])
                                                ^

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: scala dependency-injection scaldi


    【解决方案1】:

    从文档中弄清楚

    所有形式的注入期望和 Injector 的隐式实例都在范围内。如果

    您正在注入模块定义,那么它已经提供 一个给你。如果您要在自己的课程中注入,那么最好的 方法是提供隐式注入器实例作为 构造函数参数,如上例所示。

    所以代码应该是

    class Game(players: List[Player], currentPlayer: Player,
               board: Board, active: Boolean, gamePersistor: GamePersistor)(implicit inj:Injector) extends Injectable {
    
      def this(players: List[Player], currentPlayer: Player,
               board: Board, active: Boolean)(implicit inj: Injector) {
        this(players, currentPlayer, board, active, inject[GamePersistor])
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2013-06-16
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多