Grails - Making Methods Globally Available and Metaclass Programming

和自甴很熟 提交于 2020-01-15 08:15:42

问题


I inserted this line into my init() of my BootStrap class

Integer.metaClass.minutes = { 60000L * delegate }

I was then not able to use it from a Job class (Quartz plugin). Do I put this line of code somewhere else to make it a global modification?

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?


回答1:


Do I put this line of code somewhere else to make it a global modification?

Use the DelegatingMetaClass

I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way?

If you want the function to be an instance method of all classes, then you must add it to the metaClass of Object (see above). If not, simply add the function as a static method of a class, i.e. the same way you make functions globally accessible in Java.



来源:https://stackoverflow.com/questions/6454000/grails-making-methods-globally-available-and-metaclass-programming

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