【问题标题】:how to get certain property values from config file depending on the environment?如何根据环境从配置文件中获取某些属性值?
【发布时间】:2016-07-28 05:57:07
【问题描述】:

我有一些想要保留在配置文件中的键。我有两个不同的密钥,一个用于开发设置,另一个用于环境设置为生产环境。现在,在 grails 中,我们使用

从配置文件中提取这些属性值
grailsApplication.config.[name of the property in config file]

是否可以对配置文件进行条件设置,根据环境设置为生产还是开发,返回正确的键?我很感激任何帮助!谢谢!

【问题讨论】:

    标签: grails


    【解决方案1】:

    我们对不同的环境使用单独的外部配置文件的方法,然后根据如下环境将它们包含在“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}"
        }
    }
    

    【讨论】:

    • 正是我想要的
    猜你喜欢
    • 2022-09-23
    • 2019-06-05
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多