在参考文档中,我仅找到例如
class ExamplePlugin {
def doWithDynamicMethods = { applicationContext ->
application.controllerClasses.each { controllerClass ->
controllerClass.metaClass.myNewMethod = {-> println "hello world" }
}
}
}
但是我不想为这样的代码创建插件...
最佳答案
您可以在 init 闭包中的 grails-app / conf / BootStrap.groovy 文件中添加动态方法,然后应在应用程序启动时将其使用。
请注意,以这种方式添加的动态方法在单元测试中将不可用。它们将在集成测试和运行时可用。