Grails 如何根据环境从配置文件中获取某些属性值?
我想在配置文件中保留一些密钥。我有两个不同的键,一个用于开发设置,另一个用于环境设置为生产时。现在,在grails中,我们使用Grails 如何根据环境从配置文件中获取某些属性值?,grails,Grails,我想在配置文件中保留一些密钥。我有两个不同的键,一个用于开发设置,另一个用于环境设置为生产时。现在,在grails中,我们使用 grailsApplication.config.[name of the property in config file] 是否可以对配置文件进行条件设置,根据环境设置为“生产”还是“开发”,该设置将返回正确的键?谢谢你的帮助!谢谢 我们为不同的环境使用单独的外部配置文件,然后根据以下环境将它们包含在的“config.groovy”中 environments {
grailsApplication.config.[name of the property in config file]
是否可以对配置文件进行条件设置,根据环境设置为“生产”还是“开发”,该设置将返回正确的键?谢谢你的帮助!谢谢 我们为不同的环境使用单独的外部配置文件,然后根据以下环境将它们包含在的“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"]
}
}
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.util”包中提供的“Environment”,如下所示
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"]
}
}
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}"
}
}
正是我想要的