在域对象和静态范围中获取 grails 2.0.0M1 配置信息? [英] getting grails 2.0.0M1 config info in domain object, and static scope?
问题描述
如何从域对象或静态范围获取 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.
推荐答案
我会将 grailsApplication
添加到域类的元类中 - 这是我正在考虑为 2.0 final 做的事情.现在,把它放在 BootStrap.groovy
中,例如
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 in BootStrap.groovy
, e.g.
class BootStrap {
def grailsApplication
def init = { servletContext ->
for (dc in grailsApplication.domainClasses) {
dc.clazz.metaClass.getGrailsApplication = { -> grailsApplication }
dc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication }
}
}
}
然后你可以从 grailsApplication.config
访问配置,通过 grailsApplication.mainContext.getBean('foo')
或 grailsApplication.mainContext 访问 Spring bean.foo
.
Then you can access the config from grailsApplication.config
, and Spring beans via grailsApplication.mainContext.getBean('foo')
or just grailsApplication.mainContext.foo
.
这篇关于在域对象和静态范围中获取 grails 2.0.0M1 配置信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!