【问题标题】:externalize properties in grails将 grails 中的属性外部化
【发布时间】:2012-10-10 19:50:27
【问题描述】:

我正在尝试将 grails 中的一些自定义属性外部化。

我找不到关于如何将某些字符串属性外部化并在我的代码中使用它们的明确方法。 谁能帮帮我。

【问题讨论】:

标签: grails


【解决方案1】:

您必须在 config.groovy 中指定外部配置文件的位置。

例如这样:

if (!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}

// Internal Jetty: config-files are locates inside the /web-app/WEB-INF directory
if( GrailsUtil.isDevelopmentEnv() ) {
    def dir = System.properties["base.dir"]
    def f1 = "${dir}" + File.separator + "web-app" + File.separator + "WEB-INF" + File.separator + "${appName}-config.groovy"
    grails.config.locations << "file:${f1}"
}

// TIER Tomcat: config-files are locates inside Tomcat's conf-directory
else if( System.properties["catalina.home"] ) {
    def dir = System.properties["catalina.home"]
    def f1 = "${dir}" + File.separator + "conf" + File.separator + "${appName}-config.groovy"
    grails.config.locations << "file:${f1}"
}

// JBOSS, Glassfish etc
else {
    //
}

// Further choices: command line argument (-D{$appName}.config.location=xxx)
if( System.properties["${appName}.config.location"] ) {
  grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}

所以在本地开发中,配置文件位于 WEB-INF 目录下。 在生活中,您可以选择放置配置文件的位置。 如果需要,您可以以相同的方式调整代码以适用于 config.properties。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多