Grails 配置文件:最佳实践

发布于 2024-08-13 06:44:26 字数 313 浏览 3 评论 0原文

只是想知道将配置键值对添加到 grails 应用程序时的“最佳实践”是什么。

您应该只是添加到 Config.groovy 或创建新文件。

我尝试创建一个新的配置文件(Company.groovy),但无法从我的应用程序访问配置道具。然而,当我将属性粘贴到 Config.groovy 中时,我确实可以访问它们......这工作正常,但我不希望 Config.groovy 变得太大。 另外一个问题也随之出现。在运行集成测试时,我发现“测试”环境无法访问我的新配置属性(值为空)。

我一定是做了一些根本性错误的事情。任何建议将不胜感激。

谢谢, D

Just wondering what is the 'best practice' when adding config key-value pairs to your grails app.

Should you just add to Config.groovy or create new files.

I tried creating a new config file (Company.groovy) but could not access the config props from my app. However when I cup-paste the properties into Config.groovy I do have access to them.... This works fine but I dont want Config.groovy to get too large.
Also another problem raised its head. While running Integration tests I found that the 'test' env did not have access to my new config properties (values were null).

I must be doing something fundamentally wrong. Any advice would be appreciated.

Thanks,
D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年华零落成诗 2024-08-20 06:44:26

我不确定是否有具体的最佳实践。我可以告诉你,只添加一些选项的插件通常会重用 Config.groovy,但是需要更复杂配置的插件(即 Nimble)通常有自己的可以分离的文件。因此,这实际上取决于您的特定需求和您想要添加的配置数量。

如果您检查 Config.groovy,您将在最上面几行(在此处复制)中看到您可以指定 Grails 将在何处查找配置文件。它可以自动为您合并属性和 .groovy 文件。

// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts

// grails.config.locations = [ "classpath:${appName}-config.properties",
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"] 

文档中的此部分 为您描述了选项。

I am not sure there are specific best practices. I can tell you plugins that just add a few options usually reuse Config.groovy, but ones that require more complex configuration (i.e. Nimble) typically have their own file that can be separated. So it really depends on your particular needs and amounts of configuration you want to add.

If you examine Config.groovy, you will see in the few top lines (reproduced here) that you can specify where Grails will look for configuration files. It automatically can merge property and .groovy files for you.

// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts

// grails.config.locations = [ "classpath:${appName}-config.properties",
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"] 

This section in the docs describes the options for you.

泪之魂 2024-08-20 06:44:26

只是为了添加上面的答案,如果您将文件命名为 MySampleConfig.groovy (只需以 Config.groovy 结尾文件名),则应将其添加到 grailsApplication 上下文中,以便您应该能够引用以下内容:

sample{
       first{
           name = "Italy"
       }
       second{
           name = "Spain"
       }
}

作为

grailsApplication.config.sample["first"].name
grailsApplication.config.sample["second"].name

插件,我相信行为略有不同,需要使用 ConfigSlurper 或类似的(我非常愿意纠正这一点)。

def config = new ConfigSlurper().parse(new File('grails-app/conf/MySampleConfig.groovy').toURL())
println config.sample['first'].name
println config.sample['second'].name

Just to add the the answer above, if you name your file MySampleConfig.groovy (just end the file name with Config.groovy) its should be added to the grailsApplication context so you should be able to reference the following :

sample{
       first{
           name = "Italy"
       }
       second{
           name = "Spain"
       }
}

as

grailsApplication.config.sample["first"].name
grailsApplication.config.sample["second"].name

Plug ins I believe behave slightly differently and using the ConfigSlurper or Similar is required (I am very open to correction on this one).

def config = new ConfigSlurper().parse(new File('grails-app/conf/MySampleConfig.groovy').toURL())
println config.sample['first'].name
println config.sample['second'].name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文