I have a simple question but the google and stackoverflow results didn't satisfy me at all.
How can I define a Constants class like:
public class Constants {
public static final int SYSTEM_USER_ID = 1;
}
that can be called everywhere with Constants.SYSTEM_USER_ID
I tried it in grails-app/utils
and src/java
but for example I couldn't access inside a Service class.
You might consider putting these constants into Config.groovy
rather than a class. One advantage of this approach is that you can specify per-environment values for these constants. You can read the values of these using either the implicit grailsApplication
variable or the ConfigurationHolder
class.
You need to put your Constants class into a package. Grails can't find the class if its not in a package itself.
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