0

我正在尝试在以下 Grails 脚本中使用 Grails 服务

includeTargets << grailsScript("_GrailsInit")

target(loadGames: "The description of the script goes here!") {
   def listFile = new File('list.txt')

   listFile.eachLine {
      def result = ctx.getBean("bggService").search(it)
      println it + " " + result.length()
   }
}

setDefaultTarget(loadGames)

我已经看到大约十几个不同的网页,每个网页都提供不同的ctx appCtx, 和applicationContext(以及许多其他) 组合作为建议,但是它们都不起作用。通常他们会抱怨我尝试使用的上下文变量不存在。

为什么 Grails 不能像在控制器中一样使用脚本中的服务?

在 Grails 脚本中使用 Grails 服务的正确方法是什么?

4

1 回答 1

2

掌握ApplicationContextgrailsApplication可能通过bootstrap命令。包含_GrailsBootstrap脚本,然后调用configureApp ()或依赖它以使其ApplicationContext在脚本中可用:

includeTargets << grailsScript("_GrailsInit")
includeTargets << grailsScript("_GrailsBootstrap")

target(loadGames: "The description of the script goes here!") {
   depends(configureApp)

   def listFile = new File('list.txt')

   listFile.eachLine {
      //Once configureApp() called ApplicationContext can be accessed as appCtx
      def result = appCtx.getBean("bggService").search(it)
      println it + " " + result.length()
   }
}

setDefaultTarget(loadGames)
于 2014-04-10T02:59:50.523 回答