简体   繁体   中英

Access Grails domain class from GroovyShell script

I'm trying to use GroovyShell to evaluate some snippets of code from within a Grails application, but I don't seem to be able to access our Grails domain classes. For example when I try to evaluate something like this:

Eval.me("my.package.MyDomainClass.get(1)")

I see an error like this:

groovy.lang.MissingPropertyException: No such property: my for class: Script1

Any thoughts on how I can get this to work?

Thanks.

I figured out how to get this working. By default a GroovyShell instance clearly evaluates the script in its own classloader, so none of your Grails artefacts are available. There's an alternative constructor where you can pass in another classloader, so this does what I need within the context of a Grails application (when running inside a grails console , for example):

def shell = new GroovyShell(this.class.classLoader)
shell.evaluate("my.package.MyDomainClass.get(1)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM