Skip to content

Instantly share code, notes, and snippets.

@mathifonseca
Last active February 9, 2018 12:47
Show Gist options
  • Save mathifonseca/ab443f1502bfd9461943 to your computer and use it in GitHub Desktop.
Save mathifonseca/ab443f1502bfd9461943 to your computer and use it in GitHub Desktop.
GRAILS - Get configuration from Holders

Since Grails 2.0, the usage of org.codehaus.groovy.grails.commons.ConfigurationHolder has been deprecated, and you get a very annoying warning, even when it is being used by one of your plugins.

The recommended way of getting your config since then has been using dependency injection to get the grailsApplication bean into your controllers or services.

I've found a better (more concise and readable) way to get my configuration. I haven't done any serious research yet, but the only difference at first look is that instead of using DI, I'm getting my configuration from a static method in class grails.util.Holders.

So, here is an example Config.groovy:

// default grails configuration values

app.default.number = 4

// other configurations

And the way of reading that configuration from a service:

import grails.util.Holders

class FooService {

     def foo() {
     
          def defaultNumber = Holders.config.app.default.number
          
          assert defaultNumber == 4
     
     }

}
Copy link

ghost commented Oct 10, 2016

This is very good. I didn't come across this in the grails 2.3.9 documentation. Thanks for throwing it up on gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment