在脚本中使用Grails服务 [英] Using a Grails service inside a script

查看:122
本文介绍了在脚本中使用Grails服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  includeTargets<<< p> grailsS​​cript(_ GrailsInit)

target(loadGames:脚本描述在这里!){
def listFile = new File('list.txt')

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

$ b $ setDefaultTarget(loadGames)

我见过十几个不同的网页,每个网页提供了 ctx appCtx applicationContext (以及许多其他)作为建议,但是它们都不起作用。通常他们会抱怨我试图使用的上下文变量不存在。



为什么Grails不能在脚本中使用服务,方式与他们完全相同

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

解决方案

通过 ApplicationContext 和 grailsApplication http://grails.org/doc/2.3.x/ref/Command%20Line/bootstrap.htmlrel =nofollow> bootstrap 命令。包括 _GrailsBootstrap 脚本,然后调用 configureApp()或依赖它以使 ApplicationContext 可在脚本中找到:

  includeTargets<< grailsS​​cript(_ GrailsInit)
includeTargets<< grailsS​​cript(_ GrailsBootstrap)

target(loadGames:脚本描述在这里!){
depends(configureApp)

def listFile = new文件('list.txt')

listFile.eachLine {
//一旦configureApp()调用ApplicationContext可以作为appCtx访问
def result = appCtx.getBean(bggService ).search(it)
println it ++ result.length()
}
}

setDefaultTarget(loadGames)


I'm trying to use a Grails service inside the following Grails script

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)

I've seen about a dozen different webpages each offering a different combination of ctx appCtx, and applicationContext (as well as many others) as suggestions, however none of them work. Typically they complain that the context variable that I am trying to use does not exist.

Why can't Grails just use services inside a script in exactly the same way that they get used in controllers?

What is the right way to use a Grails service inside a Grails script?

解决方案

Getting hold of ApplicationContext and grailsApplication is possible though bootstrap command. Include _GrailsBootstrap script, then call configureApp () or depend on it in order to make ApplicationContext available in script:

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)

这篇关于在脚本中使用Grails服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆