我最近开始将我的一个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
这是我的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
}
}
关于如何摆脱这个错误的任何想法?
答案 0 :(得分:3)
我能够通过这样做摆脱这个错误:
private[this] class App(context: Context)
extends BuiltInComponentsFromContext(context)
with StrictLogging with _root_.controllers.AssetsComponents {
....
....
}
请注意,我使用以下软件包注入AssetComponents:
with _root_.controllers.AssetsComponents
有什么理由说它应该是这样的吗?还有什么可能是这件事呢?即使我不明白为什么它应该是这样的,我能够超越编译器。所以我将此作为解决方案发布!
答案 1 :(得分:0)
我今天遇到了类似的问题但是当我删除import play.controllers.AssetsComponents
时它已得到解决。似乎我错误地包含了这个文件,这引起了冲突。