简体   繁体   中英

Play Framework 2.6.x Compile Time DI Error

I recently started to migrate one of my Play Framework application from 2.5.9 to 2.6.12 and my application is using compile time DI. I now have some problems when upgrading. First here is the error that I face:

[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

Here is my 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
  }
}

Any ideas as to how I could get rid of this error?

I was able to get rid of this error by doing this:

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

      ....
      ....

}

Notice that I'm using the following package for injecting the AssetComponents:

with _root_.controllers.AssetsComponents

Is there any reason why it should be like this? Also what could be this _ thing? Even though I do not get why it should be like this, I was able to get past the compiler. So I'm posting this as a solution!

I faced similar issue today but it got resolved when I removed import play.controllers.AssetsComponents . It seems I included this file by mistake which caused conflicts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM