如何在Grails项目中创建全局变量 [英] How to create global variables in a Grails project

查看:177
本文介绍了如何在Grails项目中创建全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作一个可供Grails项目中的几乎所有类访问的变量的最佳做法是什么?有没有一个配置文件,我可以使用该数据的存储(即application.properties)?

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)?

谢谢。

推荐答案

你最好的选择是Config.groovy。任何类都可以访问ConfigurationHolder.getConfig(),使其成为全局变量,甚至可以为变量指定特定于环境的值。

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
   }
}

这篇关于如何在Grails项目中创建全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆