开发者

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

开发者 https://www.devze.com 2023-03-29 23:58 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消