方法无法覆盖Play Framework Scala

时间:2016-01-08 12:25:56

标签: scala playframework playframework-2.0

我正在使用guice在play Framework 2.3.8版中实现依赖注入。下面是GlobalSettings trait的getControllerInstance的实现。

import com.google.inject.{ Guice, AbstractModule }
import play.api.GlobalSettings
import services._

object Global extends GlobalSettings {  
  val injector = Guice.createInjector(new AbstractModule {
    protected def configure() {
      bind(classOf[CategoryService]).to(classOf[CategoryServiceImpl])
    }
  })

  override def getControllerInstance[A](controllerClass: Class[A]): A = {
    injector.getInstance(controllerClass)
  }
}

当我编译项目时,它会给Method覆盖Nothing错误。以下是完整的错误说明。

[error] /home/rajkumar/scalacb/PlayFrameworkCookbook/Handwritten /PFCChapter3Guice/app/Global.scala:14: method getControllerInstance overrides nothing
[error]   override def getControllerInstance[A](controllerClass: Class[A]): A = {
[error]                ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

在getControllerInstance前面删除覆盖后,编译成功。在我运行项目之后,它会抛出不同的错误。

以下是完整的错误跟踪。

! @6ol3cn5pi - Internal server error, for (GET) [/categories] ->

play.api.UnexpectedException: Unexpected   exception[ProvisionException:     Unable to provision, see the following   errors:

1) No implementation for services.CategoryService was bound.
while locating services.CategoryService
  for parameter 0 at controllers.CategoryController.<init>   (CategoryController.scala:7)
  while locating com.google.inject.Provider<controllers.CategoryController>
  for parameter 2 at router.Routes.<init>(Routes.scala:35)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router 1 error]

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为你应该先仔细检查一下你是否真的使用Play 2.3.8,你可以查看plugins.sbt来查找。

如果事实证明您实际上正在使用Play 2.4,那么我担心看起来错误消息是正确的,因为此链接显示https://www.playframework.com/documentation/2.4.x/api/java/play/GlobalSettings.html

,因为getControllerInstance不存在