How to prevent circular dependencies when using We

2020-04-21 05:51发布

问题:

After I implemented what has been suggested by an answer of my last question, I received the following error when accessing the application in the browser:

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

This error occurs because I have a class called ErrorHandler (adapted from this 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)))
}

Now the problem is that the WebJarAssets class has errorHandler: HttpErrorHandler as a parameter and ErrorHandler extends DefaultHttpErrorHandler which subsequently extends HttpErrorHandler. That's why there is a circular dependency. Any idea how I can fix that?