如何访问 Grails 2.0 中的 Grails 配置?

发布于 2024-11-30 08:39:57 字数 293 浏览 2 评论 0原文

我已经获得了最新的 Grails 2.0 里程碑,并且看到了 ConfigurationHolder 类的弃用警告:

org.codehaus.groovy.grails.commons.ConfigurationHolder

弃用消息只是说“改为使用依赖项注入”,这对我来说不是很有帮助。我了解依赖注入,但是如何使用正确的 Grails 配置连接 bean,以便可以在运行时访问它?我需要从控制器和标签以外的地方访问配置(例如 BootStrap)。

I have obtained the latest Grails 2.0 milestone, and I am seeing a deprecation warning for the ConfigurationHolder class:

org.codehaus.groovy.grails.commons.ConfigurationHolder

The deprecation message simply says "Use dependency injection instead" which is not very helpful to me. I understand dependency injection, but how can I wire up a bean with the proper Grails configuration so I can access it at runtime? I need to access the configuration from places other than my Controllers and Tags (such as BootStrap).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

避讳 2024-12-07 08:39:57
  • 如果您在支持依赖项注入的工件中需要它,只需注入 grailsApplication

    类 MyController {
        def grails应用程序
    
        def myAction = {
            def bar = grailsApplication.config.my.property
        }
    }
    
  • 如果您在 src/groovy 中的 bean 中需要它src/java,使用 conf/spring/resources.groovy

    连接它

    // src/groovy/com/example/MyBean.groovy
    类 MyBean {
        def grails应用程序
    
        def foo() {
            def bar = grailsApplication.config.my.property
        }
    }
    
    // 资源.groovy
    豆子= {
        myBean(com.example.MyBean) {
            grailsApplication = ref('grailsApplication')
            // 或使用“自动装配”
        }
    }
    
  • 在其他地方,将配置对象传递给需要它的类,或者传递所需的特定属性可能是最简单的。

    // src/groovy/com/example/NotABean.groovy
    类 NotABean {
        def foo(def 栏) {
           ...
        }
    }
    
    // 从支持 DI 的工件调用
    类我的控制器{
        def grails应用程序
        def myAction = {
            def f = new NotABean()
            f.foo(grailsApplication.config.my.property)
        }
    }
    

更新:

Burt Beckwith 最近就此写了几篇博客文章。 有人讨论在域类中使用 getDomainClass(),而其他提供了创建您自己的持有者类的选项(如果上述解决方案都不合适) 。

  • If you need it in an artifact that supports dependency injection, simply inject grailsApplication

    class MyController {
        def grailsApplication
    
        def myAction = {
            def bar = grailsApplication.config.my.property
        }
    }
    
  • If you need it in a bean in, say, src/groovy or src/java, wire it up using conf/spring/resources.groovy

    // src/groovy/com/example/MyBean.groovy
    class MyBean {
        def grailsApplication
    
        def foo() {
            def bar = grailsApplication.config.my.property
        }
    }
    
    // resources.groovy
    beans = {
        myBean(com.example.MyBean) {
            grailsApplication = ref('grailsApplication')
            // or use 'autowire'
        }
    }
    
  • Anywhere else, it's probably easiest to either pass the configuration object to the class that needs it, or pass the specific properties that are needed.

    // src/groovy/com/example/NotABean.groovy
    class NotABean {
        def foo(def bar) {
           ...
        }
    }
    
    // called from a DI-supporting artifact
    class MyController {
        def grailsApplication
        def myAction = {
            def f = new NotABean()
            f.foo(grailsApplication.config.my.property)
        }
    }
    

Update:

Burt Beckwith recently wrote a couple of blog posts on this. One discusses using getDomainClass() from within domain classes, while the other offers the option of creating your own holder class (if none of the solutions above are appropriate).

灯下孤影 2024-12-07 08:39:57

grailsApplication 的替代方案是 Holders 类,

import grails.util.Holders

def config = Holders.config

您可以直接获取配置脱离 Holders,无需注入,这对于实用程序类等非常有用。

An alternative to grailsApplication is the Holders class,

import grails.util.Holders

def config = Holders.config

You get config directly off of Holders, no injection needed, which is nice for utility classes, etc.

旧人 2024-12-07 08:39:57

您可以将“grailsApplication”注入源文件中。这是一个示例conf/Bootstrap.groovy

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        println grailsApplication.config
    }

    def destroy = {
    }
}

you can inject "grailsApplication" into your source file. here is a sample conf/Bootstrap.groovy

class BootStrap {

    def grailsApplication

    def init = { servletContext ->
        println grailsApplication.config
    }

    def destroy = {
    }
}
白云悠悠 2024-12-07 08:39:57

另一种未弃用的获取配置的方法是:

    ApplicationContext context = ServletContextHolder.servletContext.
        getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) 
        as ApplicationContext
    ConfigObject config = context.getBean(GrailsApplication).config

这适用于没有可用的注入父级的情况,例如 servlet 类或静态方法。

Another non-deprecated way to get the config is:

    ApplicationContext context = ServletContextHolder.servletContext.
        getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) 
        as ApplicationContext
    ConfigObject config = context.getBean(GrailsApplication).config

This works in situations where there is no injected parent available, such as servlet classes or static methods.

花开浅夏 2024-12-07 08:39:57

访问grails配置

  1. 您可以

    在控制器中

    类 DemoController {
        def grails应用程序
    
        def demoAction = {
            def obj = grailsApplication.config.propertyInConfig
        }
    }
    
  2. 在服务中:

    类 DemoService {
        def grails应用程序
    
        def 演示方法 = {
            def obj = grailsApplication.config.propertyInConfig
        }
    }
    
  3. 在 taglib 中:

    类 DemoTaglib {
        def grails应用程序
    
        静态命名空间=“cd”
    
        def 演示方法 = {
    
            def obj = grailsApplication.config.propertyInConfig
    
            出<<对象    
        }
    }
    

您可以在视图中调用 taglib 的此方法

  1. 在视图中:

    <代码> 
         演示
     <正文>
         ${grailsApplication.config.propertyInConfig}
     
    
     
    

You can access grails configuration

  1. In Controller

    class DemoController {
        def grailsApplication
    
        def demoAction = {
            def obj = grailsApplication.config.propertyInConfig
        }
    }
    
  2. In services :

    class DemoService {
        def grailsApplication
    
        def demoMethod = {
            def obj = grailsApplication.config.propertyInConfig
        }
    }
    
  3. In taglib :

    class DemoTaglib {
        def grailsApplication
    
        static namespace = "cd"
    
        def demoMethod = {
    
            def obj = grailsApplication.config.propertyInConfig
    
            out << obj    
        }
    }
    

You can call this method of taglib in view as <cd:demoMethod/>

  1. In view :

     <html>
         <head><title>Demo</title></head>
     <body>
         ${grailsApplication.config.propertyInConfig}
     </body>
    
     </html>
    
淡紫姑娘! 2024-12-07 08:39:57

要从域类访问,请执行以下操作:

import grails.util.Holders

// more code

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

To access from domain class do:

import grails.util.Holders

// more code

static void foo() {
  def configOption = Holders.config.myProperty
}
小伙你站住 2024-12-07 08:39:57
import grails.util.Holders

and then you can access by 
def serverUrl = Holders.grailsApplication.config.getProperty('serverUrl')
import grails.util.Holders

and then you can access by 
def serverUrl = Holders.grailsApplication.config.getProperty('serverUrl')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文