Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put a Constants class that is accessible throughout my Grails app?

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.

like image 936
grailsInvas0r Avatar asked Nov 29 '22 04:11

grailsInvas0r


2 Answers

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.

like image 159
Dónal Avatar answered Dec 05 '22 13:12

Dónal


You need to put your Constants class into a package. Grails can't find the class if its not in a package itself.

like image 38
Gambo Avatar answered Dec 05 '22 13:12

Gambo