Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global variables in a Grails project

Tags:

grails

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.

like image 592
firnnauriel Avatar asked Apr 14 '10 11:04

firnnauriel


People also ask

How do you declare a global variable?

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.

Where are global variables stored in assembly?

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.

How do you declare a global variable in SAP?

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 .


1 Answers

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
   }
}
like image 162
Burt Beckwith Avatar answered Oct 10 '22 03:10

Burt Beckwith