在域对象和静态范围中获取 grails 2.0.0M1 配置信息?

发布于 2024-11-28 12:26:34 字数 153 浏览 1 评论 0原文

如何从域对象或静态范围获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

给妤﹃绝世温柔 2024-12-05 12:26:34

Grails 2 中已弃用的 ApplicationHolderConfigurationHolder 等的替代品是 grails.util.Holders,它提供相同的功能,但当同一容器中的多个不同的 Web 应用程序共享单个副本时,该功能是安全的Grails JAR在父类加载器中(这是旧持有者崩溃的情况)。

import grails.util.Holders

// ...

static void foo() {
  def configOption = Holders.config.myapp.option
}

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).

import grails.util.Holders

// ...

static void foo() {
  def configOption = Holders.config.myapp.option
}
娇柔作态 2024-12-05 12:26:34

我将 grailsApplication 添加到域类的元类中 - 这是我正在考虑为 2.0 Final 做的事情。现在,将其放入 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 }
      }      
   }
}

然后您可以从 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 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 }
      }      
   }
}

Then you can access the config from grailsApplication.config, and Spring beans via grailsApplication.mainContext.getBean('foo') or just grailsApplication.mainContext.foo.

画中仙 2024-12-05 12:26:34

我真的只想访问静态实用程序中的配置。在搜索和阅读了 SO 上的大部分答案后,我提出了简单的解决方案(可能对某人有用):

在 src/groovy 下添加holder类:

class StaticContext {
    static def app;
}

在 bootstrap init 中初始化它

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
      StaticContext.app = grailsApplication
    }

    def destroy = {
    }
}

并在静态实用程序中访问它:

//In my case Solr URL in single ton
def solrUrl = StaticContext.app.config.solr.url

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:

class StaticContext {
    static def app;
}

initialize it in 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
酷遇一生 2024-12-05 12:26:34

在 Grails 2.2.5 中,我发现这可行:

  1. grails-app/conf/Config.groovy中配置您的变量,在适合您的环境的部分中。例如:

    环境{
    ...
      发展 {
      ...
        grails.config.myUrl =“http://localhost:3000”
        ...
    

    ...

  2. 访问:

    导入 grails.util.Holders
    
    类我的类{
    ...
    
       def 静态 myStaticMethod() {
          def myVar = Holders.config.grails.config.myUrl
    ...
    

In Grails 2.2.5 I found this would work:

  1. Configure your variable in grails-app/conf/Config.groovy, in the section appropiate for your environment. For example:

    environments {
    ...
      development {
      ...
        grails.config.myUrl = "http://localhost:3000"
        ...
    

    ...

  2. To access:

    import grails.util.Holders
    
    class myClass {
    ...
    
       def static myStaticMethod() {
          def myVar = Holders.config.grails.config.myUrl
    ...
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文