由于 "No implementation for controllers.MyExecutionContext was bound" 无法提供

Unable to provision because of "No implementation for controllers.MyExecutionContext was bound"

我正在尝试按照说明创建 Play Framework 异步控制器。到目前为止,我的代码只不过是 cut & paste from the Play documentation:

package controllers

import akka.actor.ActorSystem
import com.google.inject.Inject
import play.api.libs.concurrent.CustomExecutionContext
import play.api.mvc.{AbstractController, ControllerComponents}
import scala.concurrent.{ExecutionContext, Future}
trait MyExecutionContext extends ExecutionContext

class MyExecutionContextImpl @Inject()(system: ActorSystem)
  extends CustomExecutionContext(system, "my.executor") with MyExecutionContext

class FooController @Inject() (myExecutionContext: MyExecutionContext, cc:ControllerComponents) extends AbstractController(cc) {
  def foo = Action.async(
    Future {
      // Call some blocking API
      Ok("result of blocking call")
    }(myExecutionContext)

  )
}

当我尝试 运行 这个新控制器时,出现以下错误:

ProvisionException:无法配置,请参阅以下错误:

1) No implementation for controllers.MyExecutionContext was bound.
  while locating controllers.MyExecutionContext
    for the 1st parameter of controllers.FooController.<init>(FooController.scala:14)
  while locating controllers.FooController
    for the 4th parameter of router.Routes.<init>(Routes.scala:33)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider

谁能解释这里可能出了什么问题?

异常表明您没有将实现 (MyExecutionContext) 绑定到模块中的特征 (MyExecutionContextImpl)。

试试这个:

class Module extends AbstractModule {

  override def configure(): Unit = {
    bind(classOf[MyExecutionContext])
      .to(classOf[MyExecutionContextImpl])

  }
}

不过我没用过你的方法。我只使用默认的执行上下文,如:

class FooController @Inject()()(implicit val ec: ExecutionContext)

之前的回答无效。尝试使用 @ImplementedBy
(来源:)

此外,您需要在 conf/application.conf 中配置您的执行程序。例如尝试添加:

my.executor {
  type = Dispatcher
  executor = "thread-pool-executor"
  thread-pool-executor {
    core-pool-size-factor = 10.0
    core-pool-size-max = 10
  }
}

(来源)