Grails and Groovy metaclass package name convention

◇◆丶佛笑我妖孽 提交于 2019-12-24 02:42:52

问题


I would like to be able to place my metaclasses in the package groovy.runtime.metaclass according to the convention defined here using the delegating meta class. I have placed MyClassMetaClass in groovy.runtime.metaclass.mypackage. for the concrete class mypackage.MyClass{}. Unfortunately calls to MyClass{} are not intercepted by MyClassMetaClass{}. Perhaps this is related to how grails initializes..?


回答1:


It is possible to use a delegating meta class in a grails project by changing BootStrap.groovy.

import org.codehaus.groovy.runtime.InvokerHelper
class BootStrap {

    def init = { servletContext ->
        def myMetaClass = new MyClassMetaClass(MyClass.class)
        InvokerHelper.metaRegistry.setMetaClass(MyClass.class, myMetaClass)
    }
    def destroy = {
    }
}

Ofcourse this is not the same as implementing delegating meta classes as mentioned in the question.



来源:https://stackoverflow.com/questions/11743582/grails-and-groovy-metaclass-package-name-convention

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!