Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get at the goodies in my Grails Config.groovy at runtime?

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?

like image 562
danb Avatar asked Oct 13 '08 17:10

danb


2 Answers

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?

like image 74
khylo Avatar answered Nov 02 '22 17:11

khylo


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
like image 31
Robert Fischer Avatar answered Nov 02 '22 19:11

Robert Fischer