36

in Config.groovy I see this:

// set per-environment serverURL stem for creating absolute links
environments {
    production {
        grails.serverURL = "http://www.changeme.com"
    }
}

what is the correct way to access that at runtime?

danb
  • 10,239
  • 14
  • 60
  • 76

4 Answers4

75

In more recent versions of grails ConfigurationHolder has been deprecated.

Instead you should use the grailsApplication object.

grailsApplication.config.grails.serverURL

If in a Controller or Service then use dependency injection of grailsApplication object. e.g.

class MyController{
    def grailsApplication
    def myAction() {
        grailsApplication.config.grails.serverURL
    }

See How to access Grails configuration in Grails 2.0?

Community
  • 1
  • 1
khylo
  • 4,430
  • 3
  • 29
  • 24
  • 1
    Nice catch, ConfigurationHolder is deprecated in Grials 2.0 same as closures as "actions" :). http://grails.org/doc/2.0.x/guide/introduction.html#webFeatures – Tomo Aug 04 '12 at 13:55
  • 2
    @khylo Isn't there also the possiblity of using the `grails.util.Holders` object, which is implemented since Grails 2.0 and use `Holders.config` to get read access to the `Config.groovy` file? – herom Jan 21 '13 at 10:17
  • Yes.. thats true. .I didn't know about that class – khylo Jan 29 '13 at 13:24
31

danb is on the right track. However, life gets a bit easier on your fingers if you do a nicer import:

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
println CH.config.grails.serverURL
Robert Fischer
  • 1,436
  • 12
  • 26
  • very nice.. i was completely unaware of that feature, which I will call import aliasing. – danb Oct 13 '08 at 18:33
  • 12
    In grails 2.0 and above ConfigurationHolder (and other Holders) have been deprecated. Instead you should use dependency injection. See my answer below – khylo May 15 '12 at 09:40
  • Is there any other way to access the configuration in Util classes without Holder classes since there dependency injection is not possible? – Sebastian Barth Jun 21 '16 at 11:39
14

here it is:

import org.codehaus.groovy.grails.commons.ConfigurationHolder
println ConfigurationHolder.config.grails.serverURL

alternatively, in controllers and tags, apparently this will work:

grailsApplication.config.grails.serverURL

I needed it in BootStrap, so option 1 was what I needed.

danb
  • 10,239
  • 14
  • 60
  • 76
  • This post explains it should still be possible to use option 2 (non-deprecated approach) in Bootstrap: http://stackoverflow.com/questions/7133580/how-to-access-grails-configuration-in-grails-2-0 – pm_labs Nov 26 '12 at 12:23
10

As mentioned in a few of the comments, another option is the grails.utils.Holders class which was added in Grails 2.0. I prefer this approach since you can use it in classes that aren't configured with dependency injection.

import grails.util.Holders

class Foo {
    def bar() {
        println(Holders.config.grails.serverURL)
    }
}
jstricker
  • 2,132
  • 1
  • 30
  • 44