Search code examples
grailsgrails-plugin

How to adding Dynamic Methods at Runtime to Grails domain object without creating plugins?


In the ref documentation I found only example such as

class ExamplePlugin {
  def doWithDynamicMethods = { applicationContext ->
        application.controllerClasses.each { controllerClass ->
             controllerClass.metaClass.myNewMethod = {-> println "hello world" }
        }
  }
}

But I don't want to create plugin for such code...


Solution

  • You can add your dynamic methods in the grails-app/conf/BootStrap.groovy file in the init closure, then they should be available at app startup.

    Note that the dynamic methods you add this way won't be available in your unit tests. They will be available in integration tests and at run-time.