Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, Bootstrap.groovy may not be the best place to do this kind of metaprogramming. The problem with this approach is that the changes to the classes will be applied when the application starts, but you may lose these changes when the application is reloaded. Obviously this is only an issue during development, and not an issue at all if you don't mind restarting the server every time you make a change, but I'll bet this would quickly become a major annoyance. In order to have the changes applied when app is reloaded as well, you should move the metaprogramming into a plugin, where you can hook into the <code>onChange</code> application lifecycle event.</p> <p>So the steps are:</p> <ul> <li>Create a plugin</li> <li>Do the metaprogramming in the <code>doWithDynamicMethods</code> and <code>onChange</code> closures of the plugin descriptor</li> </ul> <p>Here's a complete example where I "override" the chain() method on all the controller classes. The code to do likewise for the save() method of domain classes should only require some obvious replacements, e.g. use <code>application.domainClasses</code> instead of <code>application.controllerClasses</code></p> <pre><code>def doWithDynamicMethods = {ctx -&gt; application.controllerClasses.each {controller -&gt; replaceChain(controller) } } def onChange = {event -&gt; if (application.isArtefactOfType(ControllerArtefactHandler.TYPE, event.source)) { def clz = application.getControllerClass(event.source?.name) replaceChain(clz) } } private replaceChain(controllerClass) { // Save a reference to the grails chain() method def grailsChain = controllerClass.metaClass.pickMethod("chain", [Map] as Class[]) controllerClass.metaClass.chain = {Map params -&gt; println "My code to execute before chain goes here" // Invoke the grails chain() method grailsChain.invoke(delegate, [params] as Object[]) println "My code to execute after chain goes here" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload