在域对象和静态范围中获取 grails 2.0.0M1 配置信息?
如何从域对象或静态范围获取 Config.groovy 信息?我现在正在使用 ConfigurationHolder.config.*,但是它和 ApplicationHolder 已被弃用,所以我想“正确执行”...但是 grailsApplication 对象在 DO/静态范围中不可用。
How do I get to the Config.groovy information from a domain object, or from a static scope? I'm using ConfigurationHolder.config.* now, but that and ApplicationHolder are deprecated so I'd like to 'do it right' ... but the grailsApplication object isn't available in a DO/static scope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Grails 2 中已弃用的
ApplicationHolder
、ConfigurationHolder
等的替代品是grails.util.Holders
,它提供相同的功能,但当同一容器中的多个不同的 Web 应用程序共享单个副本时,该功能是安全的Grails JAR在父类加载器中(这是旧持有者崩溃的情况)。The Grails 2 replacement for the deprecated
ApplicationHolder
,ConfigurationHolder
, etc. isgrails.util.Holders
, which provides the same functionality but in a way that is safe when several different webapps in the same container are sharing a single copy of the Grails JARs in a parent classloader (this being the case where the old holders broke down).我将
grailsApplication
添加到域类的元类中 - 这是我正在考虑为 2.0 Final 做的事情。现在,将其放入BootStrap.groovy
中,例如然后您可以从
grailsApplication.config
访问配置,并通过grailsApplication.mainContext.getBean(' foo')
或只是grailsApplication.mainContext.foo
。I'd add the
grailsApplication
to the metaclass of domain classes - this is something I'm thinking about doing for 2.0 final. For now, put it inBootStrap.groovy
, e.g.Then you can access the config from
grailsApplication.config
, and Spring beans viagrailsApplication.mainContext.getBean('foo')
or justgrailsApplication.mainContext.foo
.我真的只想访问静态实用程序中的配置。在搜索和阅读了 SO 上的大部分答案后,我提出了简单的解决方案(可能对某人有用):
在 src/groovy 下添加holder类:
在 bootstrap init 中初始化它
并在静态实用程序中访问它:
I really wanted to access config in static utilities only. After searching and reading most of the answers on SO, i came with simple solution(May be useful for somebody):
Add holder class under src/groovy:
initialize it in bootstrap init
And access it in static utilities :
在 Grails 2.2.5 中,我发现这可行:
在
grails-app/conf/Config.groovy
中配置您的变量,在适合您的环境的部分中。例如:...
访问:
In Grails 2.2.5 I found this would work:
Configure your variable in
grails-app/conf/Config.groovy
, in the section appropiate for your environment. For example:...
To access: