如何将 groovy 包含在 grails 项目中
我是个白痴什么的,我不知道如何将 groovy 添加到 src/groovy 并使其工作。假设我的引导程序中有一些元内容,我想将这些调用移动到一个可以从单元测试或基于此问题的任何位置调用的类: 在 grails 中进行元编程的正确方法,以便其在单元测试中可用
所以如果我把它放在我的bootstrap(顶部有 import myproject.*
)可以工作。
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
assert 3.gimmeAP() == 'p'
所以我正在使用 STS,我进入 src/groovy 并说“New GroovyClass”将其添加到打包 myproject 并按如下方式填写:
package yakit
class MetaThangs {
def doMetaThangs() {
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
}
}
然后我在引导程序中调用它:
MetaThangs.doMetaThangs()
assert 3.gimmeAP() == 'p'
我收到错误:
Running Grails application..
2012-02-07 14:12:13,332 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
at grails.util.Environment.executeForEnvironment(Environment.java:244)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at BootStrap$_closure1.doCall(BootStrap.groovy:5)
... 26 more
Application context shutting down...
Application context shutdown.
它真的告诉我我应该吗正在输入“doMetaThangs()”而不是“doMetaThangs()”?
更新:根据@mkoryak的回答,我尝试将MetaThangs.groovy中的方法声明更改为:
static def doMetaThangs(){
...
}
它一开始不起作用,但最终它出现了。
I'm an idiot or something, and I don't know how to add groovy into src/groovy and make it work. Lets say I got some meta stuff in my bootstrap and I want to move those calls to a class I can call from unit tests or wherever based on this question: Correct way to metaprogram in grails so its available in unit tests
So if I put this in my bootstrap (which has import myproject.*
at the top) it works.
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
assert 3.gimmeAP() == 'p'
So I'm using STS and I go into src/groovy and say "New GroovyClass" add it to package myproject and fill it in as so:
package yakit
class MetaThangs {
def doMetaThangs() {
ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
}
}
Then I call this in bootstrap:
MetaThangs.doMetaThangs()
assert 3.gimmeAP() == 'p'
I get the error:
Running Grails application..
2012-02-07 14:12:13,332 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
at grails.util.Environment.executeForEnvironment(Environment.java:244)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at BootStrap$_closure1.doCall(BootStrap.groovy:5)
... 26 more
Application context shutting down...
Application context shutdown.
Is it really telling me I should be typing 'doMetaThangs()' instead of 'doMetaThangs()'?
UPDATE: based on @mkoryak answer I have tried changing the method declaration in MetaThangs.groovy to:
static def doMetaThangs(){
...
}
It didn't work at first, but eventually it came around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
doMetaThangs 不是静态的,但您调用它时就好像它是静态的一样。
要么向方法添加 static 修饰符,要么在类的实例(而不是类)上调用它。
doMetaThangs is not static, but you are calling it as if it was.
either add the static modifier to the method, or call it on an instance of the class, not the class.