How to create global variables in a Grails project

前端 未结 2 2059
孤城傲影
孤城傲影 2021-02-07 22:02

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

2条回答
  •  心在旅途
    2021-02-07 22:42

    With Grails 2.2

    //In Config.groovy
    myVar = '/My/Root/Images/Folder'
    
    //In your Services/Controllers/etc..  
    import grails.util.Holders
    def grailsApplication = Holders.getGrailsApplication()
    
    //access you variable
    def myVar =  grailsApplication.config.myVar;
    

提交回复
热议问题