2016-07-28 39 views
1

我有一些密钥,我想保留在配置文件中。我有两个不同的密钥,一个用于开发环境,另一个用于环境设置为生产环境。现在,我们的Grails提取配置文件使用如何根据环境从配置文件中获取某些属性值?

grailsApplication.config.[name of the property in config file] 

是有可能对配置文件有条件设置,将返回取决于环境是否设置为生产或开发的右键这些属性值?我感谢任何帮助!谢谢!

回答

3

我们使用不同的环境不同的外部配置文件的方法,然后将它们包括在“Config.groovy中”,这取决于环境,如下面

environments { 
    test { 
     grails.logging.jul.usebridge = true 
     grails.config.locations = ["file:${userHome}/.grails/${appName}-config-TEST.groovy"] 
    } 
    development { 
     grails.logging.jul.usebridge = true 
     grails.config.locations = ["file:${userHome}/.grails/${appName}-config-DEV.groovy"] 
    } 
    production { 
     grails.logging.jul.usebridge = false 
     grails.config.locations = ["file:${userHome}/.grails/${appName}-config-PROD.groovy"] 
    } 
} 

但如果你想共同文件的所有环境,那么你可以使用“环境”提供“grails.util”包像下面

package asia.grails.myexample 
import grails.util.Environment 
class SomeController { 
    def someAction() { 
     if (Environment.current == Environment.DEVELOPMENT) { 
      // insert Development environment specific key here 
     } else 
     if (Environment.current == Environment.TEST) { 
      // insert Test environment specific key here 
     } else 
     if (Environment.current == Environment.PRODUCTION) { 
      // insert Production environment specific key here 
     } 
     render "Environment is ${Environment.current}" 
    } 
} 
+0

Exactl Ÿ我在找什么 –

相关问题