简体   繁体   English

如何运行 Grails 脚本并获取域类实例

[英]How to run Grails script and get domain class instance

How to get current application environment in Grails scripts to be able to work with classes and objects like grails console does.如何在 Grails 脚本中获取当前的应用程序环境,以便能够像grails console那样使用类和对象。 My script:我的脚本:

import security.User

List users = User.list()
users.each { User user ->
    print(user.name)
}

And I get an error:我收到一个错误:

my-script.groovy: 1: unable to resolve class security.User

All other manipulations from Creating an instance of a domain class inside a grails script also does not help. 在 grails 脚本中创建域类的实例的所有其他操作也无济于事。

I see many grails docs are stale and inconsistent.我看到许多 grails 文档陈旧且不一致。

Grails 3.x removed the run-script command so you have to write your own command. Grails 3.x 删除了run-script命令,因此您必须编写自己的命令。 Here is an answer from one of the author of the Grails community:以下是 Grails 社区作者之一的回答:

https://stackoverflow.com/a/30806256/2405040 https://stackoverflow.com/a/30806256/2405040

As an alternative, you can write an ApplicationContextCommand see for example, the schema-export command作为替代方案,您可以编写一个 ApplicationContextCommand,例如,schema-export 命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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