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.
Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.
Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program. Finally, the heap portion of memory is the part of a program's address space associated with dynamic memory allocation.
In most applications, you define global variables by selecting Information System in the application in question. The component EC-EIS is an exception. Here, you define global variables by choosing Information System Variables Define Variables in the application menu. To define a new variable, choose Edit New Entries .
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With