【问题标题】:Is it possible to load more config files from an externalized config file in Grails?是否可以从 Grails 中的外部化配置文件加载更多配置文件?
【发布时间】:2012-07-13 22:14:22
【问题描述】:

假设情况:

我从网上下载了一个 Grails 应用程序作为 WAR 文件 foo.war。在文档中它说我可以将自己的自定义配置放在/foo.groovy 中,因为此路径包含在grails.config.locations 中的Config.groovy 中。我可以将我所有的自定义配置转储到一个文件中,并且生活很好。

如何,这是我的问题... FooApp 的配置又大又多,我不想将所有内容都放在一个文件中。我想将其分解为/bar.groovy/baz.groovy 以保持井井有条。有没有办法在 /foo.groovy 中指定某些内容,以便 FooApp 也将获取 /bar.groovy/baz.groovy 并处理它们?

我已经尝试在/foo.groovy 中添加grails.config.locations 的路径,但Grails 不喜欢这样,并在启动时抛出了一个讨厌的异常。我不确定要采取什么其他方法。

为清楚起见进行编辑:

grails-app/conf/Config.groovy 看起来像这样:

grails.config.locations = ["file:/foo.groovy"]

现在不修改grails-app/conf/Config.groovy,只修改/foo.groovy,有没有办法加载除/foo.groovy以外的更多配置文件?

【问题讨论】:

  • 如果无法做到这一点,请考虑在Grails Jira 提出请求。

标签: grails configuration grails-config


【解决方案1】:

您可以在foo.groovy 中添加其他配置文件:

foo.groovy

port {
  to {
    somewhere=8080
    another {
      place=7070
    }  
  }  
}
host = new ConfigSlurper().parse(new File("bar.groovy").toURL())

bar.groovy

to {
  somewhere="http://localhost/"
  another {
    place="https://another.place.com/"
  }
}

所以在你的应用中你有:

assert grailsApplication.config.port.to.somewhere == 8080
assert grailsApplication.config.port.to.another.place == 7070
assert grailsApplication.config.host.to.somewhere == "http://localhost/"
assert grailsApplication.config.host.to.another.place == "https://another.place.com/"

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多