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

后端 未结 4 1078
慢半拍i
慢半拍i 2020-12-13 00:32

in Config.groovy I see this:

// set per-environment serverURL stem for creating absolute links
environments {
    production {
        grails.serverURL = \"h         


        
相关标签:
4条回答
  • 2020-12-13 00:53

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:54

    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
    
    0 讨论(0)
  • 2020-12-13 00:56

    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?

    0 讨论(0)
  • 2020-12-13 00:57

    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.

    0 讨论(0)
提交回复
热议问题