How do I get to the Config.groovy information from a domain object, or from a static scope? I'm using ConfigurationHolder.config.* now, but that and ApplicationHolder are deprecated so I'd like to 'do it right' ... but the grailsApplication object isn't available in a DO/static scope.
The Grails 2 replacement for the deprecated ApplicationHolder
, ConfigurationHolder
, etc. is grails.util.Holders
, which provides the same functionality but in a way that is safe when several different webapps in the same container are sharing a single copy of the Grails JARs in a parent classloader (this being the case where the old holders broke down).
import grails.util.Holders
// ...
static void foo() {
def configOption = Holders.config.myapp.option
}
I'd add the grailsApplication
to the metaclass of domain classes - this is something I'm thinking about doing for 2.0 final. For now, put it in BootStrap.groovy
, e.g.
class BootStrap {
def grailsApplication
def init = { servletContext ->
for (dc in grailsApplication.domainClasses) {
dc.clazz.metaClass.getGrailsApplication = { -> grailsApplication }
dc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication }
}
}
}
Then you can access the config from grailsApplication.config
, and Spring beans via grailsApplication.mainContext.getBean('foo')
or just grailsApplication.mainContext.foo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With