使用WebJarAssets时如何防止循环依赖?玩2.5?

时间:2016-05-05 17:52:51

标签: scala playframework sbt webjars playframework-2.5

在我实施了上一个answerquestion建议后,我在浏览器中访问应用程序时收到以下错误:

ProvisionException: Unable to provision, see the following errors:

1) Tried proxying play.api.http.HttpErrorHandler to support a circular dependency, but circular proxies are disabled.
  while locating utils.ErrorHandler
  while locating play.api.http.HttpErrorHandler
    for parameter 0 at controllers.WebJarAssets.<init>(WebJarAssets.scala:19)
  at controllers.WebJarAssets.class(WebJarAssets.scala:19)
  while locating controllers.WebJarAssets
    for parameter 5 at utils.ErrorHandler.<init>(ErrorHandler.scala:17)
  while locating utils.ErrorHandler
  while locating play.api.http.HttpErrorHandler
    for parameter 0 at router.Routes.<init>(Routes.scala:39)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

发生此错误是因为我有一个名为ErrorHandler的类(改编自此tutorial):

package utils

import javax.inject.Inject

import com.mohiva.play.silhouette.api.SecuredErrorHandler

import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.{I18nSupport,Messages,MessagesApi}
import play.api.mvc.Results._
import play.api.mvc.{Result,RequestHeader}
import play.api.routing.Router
import play.api.{OptionalSourceMapper,Configuration}

import scala.concurrent.Future

import controllers.routes

class ErrorHandler @Inject() (
  val messagesApi: MessagesApi,
  env: play.api.Environment,
  config: Configuration,
  sourceMapper: OptionalSourceMapper,
  router: javax.inject.Provider[Router],
  webJarAssets: WebJarAssets)
  extends DefaultHttpErrorHandler(env, config, sourceMapper, router)
  with SecuredErrorHandler with I18nSupport {

  override def onNotAuthenticated(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Redirect(routes.Auth.signIn())))

  override def onNotAuthorized(request: RequestHeader, messages: Messages): Option[Future[Result]] =
    Some(Future.successful(Redirect(routes.Auth.signIn()).flashing("error" -> Messages("error.accessDenied")(messages))))

  override def onNotFound(request: RequestHeader, message: String): Future[Result] = 
    Future.successful(Ok(views.html.errors.notFound(webJarAssets, request)))

  override def onServerError(request:RequestHeader, exception:Throwable):Future[Result] = 
    Future.successful(Ok(views.html.errors.serverError(webJarAssets, request, exception)))
}

现在的问题是,WebJarAssets类将errorHandler: HttpErrorHandler作为参数,ErrorHandler扩展DefaultHttpErrorHandler,后来扩展HttpErrorHandler。这就是循环依赖的原因。知道如何解决这个问题吗?

0 个答案:

没有答案