How to add dynamic methods in Runtime for a Grails domain object without creating plugins?

In the ref documentation, I found only an example, for example

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

But I do not want to create a plugin for such code ...

+3
source share
2 answers

You can add your dynamic methods to the grails-app / conf / BootStrap.groovy file in init , then they should be available when the application starts.

Note that dynamic methods that you add in this way will not be available in your unit tests. They will be available in integration tests and at runtime.

+6
source

. . ,

SomeDomain

.

SomeDomain.metaClass.newMethod  = {-> // stuff }

...;)

- . groovy

class Example {}; Example.metaClass.newMethod = {-> println 'hello'}; def e = new Example(); e.newMethod()
+2

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


All Articles