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.
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
.
grails.util.Holders
method, mentioned by Ian Roberts, the new preferred way to do this? –
Impetuosity 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
.
grails.util.Holders
method, mentioned by Ian Roberts, the new preferred way to do this? –
Impetuosity I really wanted to access config in static utilities only. After searching and reading most of the answers on SO, i came with simple solution(May be useful for somebody):
Add holder class under src/groovy:
class StaticContext {
static def app;
}
initialize it in bootstrap init
class BootStrap {
def grailsApplication
def init = { servletContext ->
StaticContext.app = grailsApplication
}
def destroy = {
}
}
And access it in static utilities :
//In my case Solr URL in single ton
def solrUrl = StaticContext.app.config.solr.url
In Grails 2.2.5 I found this would work:
Configure your variable in
grails-app/conf/Config.groovy
, in the section appropiate for your environment. For example:environments { ... development { ... grails.config.myUrl = "http://localhost:3000" ...
...
To access:
import grails.util.Holders class myClass { ... def static myStaticMethod() { def myVar = Holders.config.grails.config.myUrl ...
© 2022 - 2024 — McMap. All rights reserved.