本文介绍了Groovy Mixins?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Groovy / Grails应用中混合使用一个类,并且使用,但我一直收到错误消息。



我有一个类似如下的域类:

  class Person {
mixin(ImagesMixin)

// ...
}

它编译得很好,但由于某些原因,它不起作用。包含ImagesMixin的文件位于我的 / src / groovy / 目录中。



我使用Groovy版本1.5.7和1.6-RC1没有任何运气。有人知道我在做什么错吗?

stacktrace:

$ $ p $ > 2008-12-30 17:58:25.258 :: WARN:上下文启动失败org.mortbay.jetty.webapp.WebAppContext@562791 {/ FinalTransmission,/ home / kuccello / Development / workspaces / lifeforce / FinalTransmission / web-app }
org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/applicationContext.xml]中定义的名为'pluginManager'的bean创建时出错:init方法的调用失败;嵌套的异常是java.lang.ExceptionInInitializerError $ b $在java.security.AccessController.doPrivileged(本地方法)
在RunApp_groovy $ _run_closure2_closure7.doCall(RunApp_groovy:67)
在RunApp_groovy $ _run_closure2_closure7.doCall (RunApp_groovy)
at Init_groovy $ _run_closure6.doCall(Init_groovy:131)
在RunApp_groovy $ _run_closure2.doCall(RunApp_groovy:66)
在RunApp_groovy $ _run_closure2.doCall(RunApp_groovy)
at RunApp_groovy $ _run_closure1.doCall(RunApp_groovy:57)RunApp_groovy
$ _run_closure1.doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this $ 2 $ dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs (Gant.groovy:372)
导致:java.lang.ExceptionInInitializerError $ b $在java.lang.Class.forName0(本地方法)$ b $在java.lang.Class.forName(类。 Java的:169) (Episode.groovy)
at Episode。< clinit>(Episode.groovy)
... 13 more
引起:groovy.lang。 MissingMethodException:没有方法的签名:static Person.mixin()适用于参数类型:(java.lang.Class)values:{class ImagesMixin}
在Broadcast。< clinit>(MyClass.groovy:17)
... 17更多
2008-12-30 17:58:25.259 :: WARN:嵌套在org.springframework.beans.factory.BeanCreationException中:在ServletContext中定义的名为'pluginManager'的bean创建时出错资源[/WEB-INF/applicationContext.xml]:调用init方法失败;嵌套异常是java.lang.ExceptionInInitializerError:
groovy.lang.MissingMethodException:没有方法的签名:Person.mixin()适用于参数类型:(java.lang.Class)values:{class ImagesMixin}
at Broadcast。< clinit>(Person.groovy:17)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169 )
at Episode.class $(BelongsToMyClass.groovy)
at Episode。< clinit>(BelongsToMyClass.groovy)
at java.security.AccessController.doPrivileged(Native Method)
在RunApp_groovy $ _run_closure2_closure7.doCall(RunApp_groovy:67)
在RunApp_groovy $ _run_closure2_closure7.doCall(RunApp_groovy)
在Init_groovy $ _run_closure6.doCall(Init_groovy:131)
在RunApp_groovy $ _run_closure2.doCall (RunApp_groovy:66)RunApp_groovy上的
$ RunSt_groovy上的
(RunApp_groovy)RunApp_groovy上的
$ Run_groovy $ Run $ _run_closure1.doCall(RunApp_groovy:57)
。 doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this $ 2 $ dispatch(Gant.groovy)
at gant.Gant.invokeMethod( Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs(Gant.groovy:372)
2008-12-30 17:58 :25.271 :: INFO:Started [email protected]:8080


解决方案
自从Groovy 1.6开始,你可以在编译时将一个mixin应用到一个类中,使用注释

  @Mixin(ImagesMixin )
class Person {
}

或者你可以在运行时应用mixin像这样:

  def myMixin = ImagesMixin 
Person.mixin myMixin

后一种方法更加动态,因为mixin类可以在运行时确定。有关Groovy mixins的更多信息,请参见。



根据我的经验,很多领域类的元编程根本无法工作。我不完全知道为什么,但怀疑这是由于这些类已经被Grails运行时重编程了。一般来说,我的方法是在Groovy控制台中试用POGO的元编程。
$ b


  • 如果可行,请在Grails控制台的非域名类中尝试使用它。

  • 如果可行,请在Grails控制台的域类中尝试。如果它不起作用,那么它一定是因为它是一个域类(而不是语法上的问题)。在这一点上,建议尝试找到实现目标的另一种方式。如果这是不可能的,那么使用Grails邮件列表和/或Stackoverflow和/或Grails源代码的组合来尝试获取元编程的工作。

I'm trying to mix-in a class in my Groovy/Grails app, and I'm using the syntax defined in the docs, but I keep getting an error.

I have a domain class that looks like this:

class Person {
  mixin(ImagesMixin)

  // ...
}

It compiles fine, but for some reason it won't work. The file containing ImagesMixin is located in my /src/groovy/ directory.

I've tried it using Groovy versions 1.5.7 and 1.6-RC1 without any luck. Does anyone know what I'm doing wrong?

stacktrace:

2008-12-30 17:58:25.258::WARN:  Failed startup of context org.mortbay.jetty.webapp.WebAppContext@562791{/FinalTransmission,/home/kuccello/Development/workspaces/lifeforce/FinalTransmission/web-app}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError
    at java.security.AccessController.doPrivileged(Native Method)
    at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
    at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
    at Init_groovy$_run_closure6.doCall(Init_groovy:131)
    at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
    at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
    at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
    at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
    at gant.Gant.dispatch(Gant.groovy:271)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.processTargets(Gant.groovy:436)
    at gant.Gant.processArgs(Gant.groovy:372)
Caused by: java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at Episode.class$(Episode.groovy)
    at Episode.<clinit>(Episode.groovy)
    ... 13 more
Caused by: groovy.lang.MissingMethodException: No signature of method: static Person.mixin() is applicable for argument types: (java.lang.Class) values: {class ImagesMixin}
    at Broadcast.<clinit>(MyClass.groovy:17)
    ... 17 more
2008-12-30 17:58:25.259::WARN:  Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.ExceptionInInitializerError:
groovy.lang.MissingMethodException: No signature of method: Person.mixin() is applicable for argument types: (java.lang.Class) values: {class ImagesMixin}
    at Broadcast.<clinit>(Person.groovy:17)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at Episode.class$(BelongsToMyClass.groovy)
    at Episode.<clinit>(BelongsToMyClass.groovy)
    at java.security.AccessController.doPrivileged(Native Method)
    at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
    at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
    at Init_groovy$_run_closure6.doCall(Init_groovy:131)
    at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
    at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
    at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
    at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
    at gant.Gant.dispatch(Gant.groovy:271)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.processTargets(Gant.groovy:436)
    at gant.Gant.processArgs(Gant.groovy:372)
2008-12-30 17:58:25.271::INFO:  Started [email protected]:8080
解决方案

Since Groovy 1.6 you can either apply a mixin at compile-time to a class using an annotation

@Mixin(ImagesMixin)
class Person {
}

Or you can apply the mixin at runtime like this:

def myMixin = ImagesMixin
Person.mixin myMixin

The latter approach is more dynamic as the class to mixin can be determined at runtime. Further information about Groovy mixins is available here.

In my experience, a lot of meta-programming of domain classes simply doesn't work. I don't exactly know why, but suspect it's due to the fact these classes are already very heavily meta-programmed by the Grails runtime. In general my approach is

  • Try the meta-programming on a POGO in the Groovy console
  • If that works, try it on a non-domain class in the Grails console
  • If that works, try it on a domain class in the Grails console. If it doesn't work, then it must be because it's a domain class (rather than a problem with the syntax). At this point it's advisable to try and find another way of accomplishing your goal. If that's not possible, then use a combination of the Grails mailing list and/or Stackoverflow and/or the Grails source code to try and get the meta-programming working.

这篇关于Groovy Mixins?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:31