Play Framework 2.6.x 编译时 DI 错误

spa*_*rkr 1 dependency-injection scala routes playframework

我最近开始将我的 Play Framework 应用程序之一从 2.5.9 迁移到 2.6.12,并且我的应用程序正在使用编译时 DI。我现在在升级时遇到了一些问题。首先是我面临的错误:

[error] /scala-projects/plant-simulator/app/com/inland24/plantsim/core/Bootstrap.scala:40: overriding method environment in trait AssetsComponents of type ()play.Environment;
[error]  lazy value environment in class BuiltInComponentsFromContext of type play.api.Environment has incompatible type;
[error]  other members with override errors are: applicationLifecycle, httpErrorHandler, fileMimeTypes
[error]   private[this] class App(context: Context)
[error]                       ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 19 s, completed Mar 3, 2018 8:43:49 AM
Run Code Online (Sandbox Code Playgroud)

这是我的 Bootstrap.scala:

import com.inland24.plantsim.controllers.{ApplicationController, PowerPlantController, PowerPlantOperationsController}
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.{LazyLogging, StrictLogging}
import play.api.{Application, BuiltInComponentsFromContext, Configuration, _}
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.ApplicationLoader.Context
import play.controllers.AssetsComponents

// these two imports below are needed for the routes resolution
import play.api.routing.Router
import router.Routes

import scala.concurrent.Future


/**
  * Bootstrap the application by performing a compile time DI
  */
final class Bootstrap extends ApplicationLoader with LazyLogging {

  private[this] class App(context: Context)
    extends BuiltInComponentsFromContext(context)
      with StrictLogging with AssetsComponents {

    // We use the Monix Scheduler
    implicit val s = monix.execution.Scheduler.Implicits.global

    def stop(bindings: AppBindings) = {
      logger.info("Stopping application :: plant-simulator")
      bindings.globalChannel.onComplete()
    }

    def start = {
      logger.info("Starting application :: plant-simulator")
      AppBindings(actorSystem, materializer)
    }

    // 0. Set the filters
    lazy val loggingFilter: LoggingFilter = new LoggingFilter()
    override lazy val httpFilters = Seq(loggingFilter)

    override val configuration = context.initialConfiguration

    // 1. create the dependencies that will be injected
    lazy val appBindings = start

    // 2. inject the dependencies into the controllers
    // TODO: The dependecies below are for Swagger UI, which is not working at the moment!!!!
    //lazy val apiHelpController = new ApiHelpController(DefaultControllerComponents)
    //lazy val webJarAssets = new WebJarAssets(httpErrorHandler, configuration, environment)
    lazy val applicationController = new ApplicationController(appBindings.appConfig, controllerComponents)
    lazy val powerPlantController = new PowerPlantController(appBindings, controllerComponents)
    lazy val powerPlantOpsController = new PowerPlantOperationsController(appBindings, controllerComponents)
    //lazy val assets = new Assets(httpErrorHandler)
    override def router: Router = new Routes(
      httpErrorHandler,
      assets,
      applicationController,
      powerPlantController,
      powerPlantOpsController
      //apiHelpController,
      //webJarAssets
    )

    // 3. add the shutdown hook to properly dispose all connections
    applicationLifecycle.addStopHook { () => Future(stop(appBindings)) }

    override def config(): Config = configuration.underlying
  }

  override def load(context: Context): Application = {
    val configuration = Configuration(ConfigFactory.load())

    val newContext = context.copy(initialConfiguration = configuration)
    LoggerConfigurator(newContext.environment.classLoader)
      .foreach(_.configure(newContext.environment))

    new App(newContext).application
  }
}
Run Code Online (Sandbox Code Playgroud)

关于如何摆脱这个错误的任何想法?

spa*_*rkr 5

通过这样做,我能够摆脱这个错误:

private[this] class App(context: Context)
    extends BuiltInComponentsFromContext(context)
      with StrictLogging with _root_.controllers.AssetsComponents {

      ....
      ....

}
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用以下包来注入 AssetComponents:

with _root_.controllers.AssetsComponents
Run Code Online (Sandbox Code Playgroud)

有什么理由让它像这样吗?还有这_东西是什么?尽管我不明白为什么会这样,但我还是通过了编译器。所以我发布这个作为解决方案!