无法使用guice进行依赖关系注射的play应用程序

时间:2019-03-23 04:30:59

标签: scala dependency-injection playframework guice

我正在尝试运行一个使用guice进行依赖注入的播放应用程序。

我的控制器如下

import scala.concurrent.ExecutionContext
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc._

class MyControllers @Inject()(
    service: MyService,
    cc: MessagesControllerComponents)(implicit ec: ExecutionContext)
    extends MessagesAbstractController(cc) {

  def getAll(id: Int): Action[AnyContent] = Action.async { implicit request =>
      // code to fetch from service and return result
  }

类似地,在我的服务中,我已将DAO注入为

class MyService @Inject()(dao: MyDAO)(implicit ec: ExecutionContext) {

// call to DB

}

我的build.sbt文件看起来像这样

name := """service-application"""

version := "2.6.x"

scalaVersion := "2.12.7"

crossScalaVersions := Seq("2.11.12", "2.12.4")

libraryDependencies ++= Seq(
  guice,
  "com.typesafe.play" %% "play-slick" % "3.0.3",
  "com.typesafe.play" %% "play-slick-evolutions" % "3.0.3",
  "org.mockito" % "mockito-all" % "1.10.19" % Test,
  "com.h2database" % "h2" % "1.4.197",
  "org.scalactic" %% "scalactic" % "3.0.5",
  "org.scalatest" %% "scalatest" % "3.0.5" % "test",
  "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test,
  "net.codingwell" %% "scala-guice" % "4.1.0",
  "com.typesafe.slick" %% "slick-codegen" % "3.2.1",
  "com.typesafe.slick" %% "slick-testkit" % "3.2.3" % Test,
  specs2 % Test
)

addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.5.1")

routesGenerator := InjectedRoutesGenerator

但是,现在,当我尝试使用命令

通过sbt shell运行play应用程序时
run

我收到错误消息

[IJ]sbt:service-application> run
[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:27)
[error] (Compile / bgRun) No main class detected.
[error] Total time: 1 s

请让我知道我在这里想念的吗? conf文件中缺少我的某种配置吗?

先谢谢了!

0 个答案:

没有答案