Lev*_*anu 2 scala playframework-evolutions playframework-2.5
我试图了解如何使用编译时DI运行演变.
import play.api.ApplicationLoader.Context
import play.api.cache.EhCacheComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.api._
import play.api.db.evolutions.{ DynamicEvolutions, EvolutionsComponents}
import play.filters.gzip.GzipFilter
import router.Routes
class AppLoader extends ApplicationLoader {
override def load(context: Context): Application = {
LoggerConfigurator(context.environment.classLoader).foreach(_.configure(context.environment))
new AppComponents(context).application
}
}
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with EhCacheComponents with EvolutionsComponents {
lazy val applicationController = new controllers.Application(defaultCacheApi)
lazy val usersController = new controllers.Users(defaultCacheApi)
lazy val assets = new controllers.Assets(httpErrorHandler)
applicationEvolutions
// Routes is a generated class
override def router: Router = new Routes(httpErrorHandler, applicationController, usersController, assets)
val gzipFilter = new GzipFilter(shouldGzip =
(request, response) => {
val contentType = response.header.headers.get("Content-Type")
contentType.exists(_.startsWith("text/html")) || request.path.endsWith("jsroutes.js")
})
override lazy val httpFilters: Seq[EssentialFilter] = Seq(gzipFilter)
}
Run Code Online (Sandbox Code Playgroud)
但我一直得到错误错误:(19,7)类AppComponents需要是抽象的,因为类型=> play.api.db.DBApi的trait EvolutionsComponents中的方法dbApi未定义类AppComponents(context:Context)扩展BuiltInComponentsFromContext(context使用带有EvolutionsComponents的EhCacheComponents
我是斯卡拉的新手.
dbApi来自这个DBComponents特性,所以你的AppComponents课程也需要扩展DBComponents.您还需要扩展HikariCPComponents连接池.
class AppComponents(context: Context) extends BuiltInComponentsFromContext(context)
with EhCacheComponents
with EvolutionsComponents
with DBComponents
with HikariCPComponents {
Run Code Online (Sandbox Code Playgroud)
请务必将evolutions和jdbc依赖项添加到您的build.sbt文件中.