我想将一些 key 保留在配置文件中。我有两个不同的 key ,一个用于开发设置,另一个用于将环境设置为生产时使用。现在,在 grails 中,我们使用
从配置文件中提取这些属性值grailsApplication.config.[name of the property in config file]
是否可以对配置文件进行条件设置,根据环境设置为生产还是开发来返回正确的 key ?我很感激任何帮助!谢谢!
最佳答案
我们针对不同的环境使用单独的外部配置文件的方法,然后根据如下环境将它们包含在“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”包中提供的“Environment”,如下所示
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}"
}
}
关于grails - 如何根据环境从配置文件中获取某些属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38628131/