Grails > 設定ファイルの参照

更新日 2014-12-07
広告

任意のプロパティを追加

grails-app/conf/Config.groovy に、任意のプロパティを追加できます。 例えば、以下のようなプロパティを追加したとします。
show.link=true
このプロパティは、'grailsApplication.config.'を頭につけることで、アクセスできます。具体的には以下のようになります。
grailsApplication.config.show.link
もう1つ、例を紹介します。
foo.bar.hello = 'Hello!'
これは、以下のように参照できます。
class SampleController {
  def myMethod() {
    def message = grailsApplication.config.foo.bar.hello
      :
  }
}
staticメソッドから参照する場合は以下のように、Holdersクラスを使います。
import grails.util.Holders

class SampleClass { 

  static void staticMethod() { 
    def grailsApplication = Holders.getGrailsApplication()
    def message = grailsApplication.config.foo.bar.hello
    :
広告
お問い合わせは sweng.tips@gmail.com まで。
inserted by FC2 system