无法提供,因为“没有实施控制器.MyExecutionContext被绑定”

问题描述 投票:1回答:2

我正在尝试按照说明创建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

任何人都可以解释这里可能出现的问题吗?

scala playframework guice
2个回答
1
投票

该异常表示您尚未将实现(MyExecutionContext)绑定到模块中的特征(MyExecutionContextImpl)。

试试这个:

class Module extends AbstractModule {

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

  }
}

但是我从未使用过你的方法。我只使用默认的执行上下文,如:

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

0
投票

之前的答案不起作用。尝试使用@ImplementedBy (来源:https://stackoverflow.com/a/53162884/4965515

此外,您还需要在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
  }
}

(来源https://stackoverflow.com/a/46013937/4965515

© www.soinside.com 2019 - 2024. All rights reserved.