[英]getting grails 2.0.0M1 config info in domain object, and static scope?
How do I get to the Config.groovy information from a domain object, or from a static scope? 如何从域对象或静态范围获取Config.groovy信息? 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.
我现在正在使用ConfigurationHolder.config。*,但是这个和ApplicationHolder已被弃用,所以我想'做对'...但是grailsApplication对象在DO /静态范围内不可用。
The Grails 2 replacement for the deprecated ApplicationHolder
, ConfigurationHolder
, etc. is grails.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). 不推荐使用的
ApplicationHolder
, ConfigurationHolder
等的Grails 2替代品是grails.util.Holders
,它提供相同的功能,但是当同一容器中的多个不同的grails.util.Holders
共享一个Grails JAR的副本时,这种方式是安全的。父类加载器(这是旧持有者崩溃的情况)。
import grails.util.Holders
// ...
static void foo() {
def configOption = Holders.config.myapp.option
}
I'd add the grailsApplication
to the metaclass of domain classes - this is something I'm thinking about doing for 2.0 final. 我将
grailsApplication
添加到域类的元类中 - 这是我正在考虑为2.0最终做的事情。 For now, put it in BootStrap.groovy
, eg 现在,把它放在
BootStrap.groovy
,例如
class BootStrap {
def grailsApplication
def init = { servletContext ->
for (dc in grailsApplication.domainClasses) {
dc.clazz.metaClass.getGrailsApplication = { -> grailsApplication }
dc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication }
}
}
}
Then you can access the config from grailsApplication.config
, and Spring beans via grailsApplication.mainContext.getBean('foo')
or just grailsApplication.mainContext.foo
. 然后,您可以通过
grailsApplication.mainContext.getBean('foo')
或grailsApplication.mainContext.foo
访问grailsApplication.config
的配置和Spring bean。
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):
在SO上搜索和阅读大部分答案后,我得到了简单的解决方案(可能对某些人有用):
Add holder class under src/groovy: 在src / groovy下添加holder类:
class StaticContext {
static def app;
}
initialize it in bootstrap init 在bootstrap init中初始化它
class BootStrap {
def grailsApplication
def init = { servletContext ->
StaticContext.app = grailsApplication
}
def destroy = {
}
}
And access it in static utilities : 并在静态实用程序中访问它:
//In my case Solr URL in single ton
def solrUrl = StaticContext.app.config.solr.url
In Grails 2.2.5 I found this would work: 在Grails 2.2.5中我发现这可行:
Configure your variable in grails-app/conf/Config.groovy
, in the section appropiate for your environment. 在
grails-app/conf/Config.groovy
,在适用于您的环境的部分中配置变量。 For example: 例如:
environments { ... development { ... grails.config.myUrl = "http://localhost:3000" ...
... ...
To access: 要访问:
import grails.util.Holders class myClass { ... def static myStaticMethod() { def myVar = Holders.config.grails.config.myUrl ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.