grails - 如何根据环境从配置文件中获取某些属性值?

标签 grails

我想将一些 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/

相关文章:

json - Grails:无法解析json并出现错误没有方法签名:static grails.converters.JSON.parse()

Grails 服务不是事务性的?

grails - Grails渲染插件(pdf)无法渲染Checkbox输入

mongodb - 由于 grails-datastore-gorm-plugin-support :jar:3. 1.3.BUILD-SNAPSHOT,Hudson 中的构建失败不一致

jQuery 对话框和 960 网格系统

grails - grails 服务中 cxf-bean/客户端的依赖注入(inject)不起作用 - 可能是因为它是在我的插件 config.groovy 中配置的?

amazon-web-services - 为什么 grails websockets 连接失败,但在使用 AWS ELB 时继续接收浏览器中已订阅的消息?

java - (无法延迟初始化集合,没有 session 或 session 已关闭)grails 1.3.7 中的 quartz-0.4.2 错误

java - 如果我已经完成了 ORM,我还需要 Web 应用程序框架吗?

grails - Grails上的MVC架构