开发者

Where to properly initialize Groovy metaclasses?

开发者 https://www.devze.com 2023-03-24 04:13 出处:网络
In a Groovy application, if you want to initialize metaclasses, where is the best place to put those initializations?In Grails apps, I\'ve used the Bootstrap.groovy file.Is there something similar for

In a Groovy application, if you want to initialize metaclasses, where is the best place to put those initializations? In Grails apps, I've used the Bootstrap.groovy file. Is there something similar for an arbitrary Groovy app?

Edit: To clarify, I'm looking for a non-intrusive way of doing this that will guarantee it gets called when running the main application 开发者_如何学编程as well as in unit tests of the associated code.


There's nothing like Bootstrap.groovy for standard groovy applications, but that doesn't stop you doing the same thing in your app and having a class or method that is called once on startup where you can add the metaClass initialization


Added from comments below:

You can write a DelegatingMetaClass for each class you wish to decorate, and place it in the package groovy.runtime.metaclass.[YOURPACKAGE].[YOURCLASS]MetaClass

This page explains the steps required to do this (2nd item on the page)

0

精彩评论

暂无评论...
验证码 换一张
取 消