有没有办法在Groovy中参数化mixin?

有没有办法在Groovy中参数化mixin?,groovy,mixins,Groovy,Mixins,我可以用参数化构造函数混入一个类吗 大概是这样的: class RenderedWithTemplates { def templates = [] RenderedWithTemplates(templates) { ... } ... } @Mixin(RenderedWithTemplates(show: "showAddress.gsp", add: "addAddress.gsp") class Address { ... } 我找到了2007年[0]的mi

我可以用参数化构造函数混入一个类吗

大概是这样的:

class RenderedWithTemplates {
   def templates = []

   RenderedWithTemplates(templates) { ... }

   ...

}

@Mixin(RenderedWithTemplates(show: "showAddress.gsp", add: "addAddress.gsp")
class Address { ... }

我找到了2007年[0]的mixin建议,但是GroovyDefaultMethodsmixin[1]方法不支持参数化mixin,也不支持@mixin

从上面的代码示例可以看出,您需要找到一种方法来混合绑定到域类的GSP视图信息。另一个稍微有点时髦的选择;这种情况下的方法是实现RenderedWithTemplates作为注释,并使用一个包含GSP视图信息的闭包参数:

import java.lang.annotation.*

@Retention(RetentionPolicy.RUNTIME)
@interface RenderedWithTemplates {
    Class value()
}

@RenderedWithTemplates({ [show: "showAddress.gsp", add: "addAddress.gsp"] }) 
class Address {}

// shows how to read the map with all the GSP infos

def templateInfo = Address.getAnnotation(RenderedWithTemplates)
def gspMap = templateInfo.value().newInstance(this, this).call()
[0]


[1]

我找到了2007年[0]的mixin建议,但是GroovyDefaultMethodsmixin[1]方法不支持参数化mixin,也不支持@mixin

从上面的代码示例可以看出,您需要找到一种方法来混合绑定到域类的GSP视图信息。另一个稍微有点时髦的选择;这种情况下的方法是实现RenderedWithTemplates作为注释,并使用一个包含GSP视图信息的闭包参数:

import java.lang.annotation.*

@Retention(RetentionPolicy.RUNTIME)
@interface RenderedWithTemplates {
    Class value()
}

@RenderedWithTemplates({ [show: "showAddress.gsp", add: "addAddress.gsp"] }) 
class Address {}

// shows how to read the map with all the GSP infos

def templateInfo = Address.getAnnotation(RenderedWithTemplates)
def gspMap = templateInfo.value().newInstance(this, this).call()
[0]

[1]

你试过了吗?你试过了吗?