Getting Grails 2.0.0M1 configuration information in a domain object and static scope?

How to get Config.groovy information from a domain object or from a static scope? I am using ConfigurationHolder.config. * Now, but this and the ApplicationHolder are deprecated, so I would like to "do it right" ... but the grailsApplication object is not available in the DO / static scope.

+6
source share
4 answers

I would add grailsApplication to the domain class metaclass - this is what I am thinking of doing for version 2.0. For now, put it in BootStrap.groovy , for example.

 class BootStrap { def grailsApplication def init = { servletContext -> for (dc in grailsApplication.domainClasses) { dc.clazz.metaClass.getGrailsApplication = { -> grailsApplication } dc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication } } } } 

You can then access the config from grailsApplication.config and Spring beans via grailsApplication.mainContext.getBean('foo') or just grailsApplication.mainContext.foo .

+8
source

Replacing Grails 2 for legacy ApplicationHolder , ConfigurationHolder , etc. grails.util.Holders , which provides the same functionality, but in such a way that it is when several different webapps in the same container share the same copy of the Grails JAR in the parent classloader (this is the case when the old holders broke).

 import grails.util.Holders // ... static void foo() { def configOption = Holders.config.myapp.option } 
+19
source

I really wanted to access only the settings in static utilities. After searching and reading most of the answers on SO, I came up with a simple solution (may be useful for someone):

Add owner class under src / groovy:

 class StaticContext { static def app; } 

initialize it at boot

 class BootStrap { def grailsApplication def init = { servletContext -> StaticContext.app = grailsApplication } def destroy = { } } 

And access to it in static utilities:

 //In my case Solr URL in single ton def solrUrl = StaticContext.app.config.solr.url 
+1
source

In Grails 2.2.5, I found this to work:

  • Set your variable in grails-app/conf/Config.groovy , in the section that matches your environment. For instance:

     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 ... 
+1
source

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


All Articles