tags:

views:

268

answers:

1

What's the best practice of making a variable that would be accessible by almost all classes in a Grails project? Is there a config file that I can use for storage of that data (i.e. application.properties)?

Thanks.

+5  A: 

Your best bet is Config.groovy. Any class can access ConfigurationHolder.getConfig() which makes it global, and you can even have environment-specific values of your variable.

someVar = "foo"

environments {
   production {
      grails.serverURL = "http://www.changeme.com"
      someOtherVar = 1000
   }
   development {
      grails.serverURL = "http://localhost:8080/${appName}"
      someOtherVar = 100
   }
   test {
      grails.serverURL = "http://localhost:8080/${appName}"
      someOtherVar = 0
   }
}
Burt Beckwith
great! just tested it and indeed it's working. just adding the info on how to get the 'someVar' value: String var = ConfigurationHolder.getConfig().getProperty('someVar'). again, thanks a lot!
firnnauriel
In Groovy you can use the aliased import "import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH" and then access it as "CH.config.someVar"
Burt Beckwith