Grails 配置文件:最佳实践
只是想知道将配置键值对添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定是否有具体的最佳实践。我可以告诉你,只添加一些选项的插件通常会重用 Config.groovy,但是需要更复杂配置的插件(即 Nimble)通常有自己的可以分离的文件。因此,这实际上取决于您的特定需求和您想要添加的配置数量。
如果您检查 Config.groovy,您将在最上面几行(在此处复制)中看到您可以指定 Grails 将在何处查找配置文件。它可以自动为您合并属性和 .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.
This section in the docs describes the options for you.
只是为了添加上面的答案,如果您将文件命名为 MySampleConfig.groovy (只需以 Config.groovy 结尾文件名),则应将其添加到 grailsApplication 上下文中,以便您应该能够引用以下内容:
作为
插件,我相信行为略有不同,需要使用 ConfigSlurper 或类似的(我非常愿意纠正这一点)。
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 :
as
Plug ins I believe behave slightly differently and using the ConfigSlurper or Similar is required (I am very open to correction on this one).