本文介绍了在域对象中获取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.

推荐答案

我会将 grailsApplication 添加到域类的元类中 - 这是我正在考虑为2.0做最后的决定。现在,把它放在 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 和Spring beans通过 grailsApplication.mainContext.getBean('foo')或者 grailsApplication.mainContext.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配置信息和静态作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 08:47