Scala Play Guice 依赖注入(inject)失败

标签 scala playframework playframework-2.0 guice

我正在尝试使用 Guice 作为 DI 在 Scala 中构建一个 Play 应用程序。该应用程序在执行 sbt run 时似乎工作正常,但是在运行 jar 时我得到了;

Oops, cannot start the server.
java.lang.RuntimeException: No application loader is configured. Please configure an application loader either using the play.application.loader configuration property, or by depending on a module that configures one. You can add the Guice support module by adding "libraryDependencies += guice" to your build.sbt.
at scala.sys.package$.error(package.scala:30)
at play.api.ApplicationLoader$.play$api$ApplicationLoader$$loaderNotFound(ApplicationLoader.scala:48)
at play.api.ApplicationLoader$.apply(ApplicationLoader.scala:143)
at play.core.server.ProdServerStart$.start(ProdServerStart.scala:56)
at play.core.server.ProdServerStart$.main(ProdServerStart.scala:29)
at play.core.server.ProdServerStart.main(ProdServerStart.scala)

我的 build.sbt 中有 libraryDependencies += guice,所以我不确定为什么会出现此错误。

任何帮助将不胜感激,我正在使用 play 2.7。

Build.sbt 文件:

name := "my-project"
organization := "com.mycompany"

version := "1.0.0"

lazy val root = (project in file("."))
  .enablePlugins(PlayScala)

scalaVersion := "2.12.8"

libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.1" % Test
libraryDependencies += "commons-codec" % "commons-codec" % "1.12"
libraryDependencies += "org.javaswift" % "joss" % "0.10.2"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
libraryDependencies += "com.amazonaws" % "aws-java-sdk-s3" % "1.11.519"
libraryDependencies += "com.softwaremill.macwire" %% "macros" % "2.3.1" % "provided"
libraryDependencies += "com.softwaremill.macwire" %% "util" % "2.3.1"

// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.realitymine.controllers._"

// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.realitymine.binders._"

mainClass in assembly := Some("play.core.server.ProdServerStart")
fullClasspath in assembly += Attributed.blank(PlayKeys.playPackageAssets.value)

assemblyMergeStrategy in assembly := {
  case manifest if manifest.contains("MANIFEST.MF") =>
    // We don't need manifest files since sbt-assembly will create
    // one with the given settings
    MergeStrategy.discard
  case referenceOverrides if referenceOverrides.contains("reference-overrides.conf") =>
    // Keep the content for all reference-overrides.conf files
    MergeStrategy.concat
//  case x =>
//    // For all the other files, use the default sbt-assembly merge strategy
//    val oldStrategy = (assemblyMergeStrategy in assembly).value
//    oldStrategy(x)
  case PathList("META-INF", xs @ _*) => MergeStrategy.discard
  case x => MergeStrategy.first
}

test in assembly := {}```


最佳答案

如评论中所述,我遇到了同样的问题并在此处找到了解决方案。 Merge Strategy in sbt assembly and missing application loader

关于Scala Play Guice 依赖注入(inject)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355403/

相关文章:

java - 在 Play 上显示错误! 2 框架

java - 在Application.java中按名称返回结果 View

scala - 什么时候应该使用 Scala 方法和函数

scala - 如何强制 F[_] 成为 Monad 的实例

scala - 读取 Casbah MongoDB 查询的结果

java - 比较所有模型对象

bash - Scala Bash 测试脚本

scala - JMX 与 Play 2.XX 框架

java - SRC = 路由 Assets (用户连接的动态 URL)?

playframework - 在Play Framework 2.0上运行进化