Grails - Global Development Methods and Metaclasses

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

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

Then I could not use it from the Job class (Quartz plugin). Am I putting this line of code in another place to make it a global modification?

I was also interested in how best to use the function in all classes in Grails. Like a global function. Will there be an extension to the metaclass of the object? or is there a better way?

+2
source share
1 answer

Am I putting this line of code in another place to make it a global modification?

Use DelegatingMetaClass

I was also interested in how best to use the function in all classes in Grails. Like a global function. Will there be an extension to the metaclass of the object? or is there a better way?

If you want the function to be an instance method for all classes, you must add it to the metaClass Object (see above). If not, just add the function as a static method of the class, that is, just like you execute globally accessible functions in Java.

+1
source

Source: https://habr.com/ru/post/1391307/


All Articles