本文介绍了如何在Grails 2.0中访问Grails配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  org.codehaus.groovy.grails.commons.ConfigurationHolder 

弃用消息只是说使用依赖注入,这对我没有什么帮助。我理解依赖注入,但是我怎样才能用适当的Grails配置连接bean,以便在运行时访问它?我需要从我的控制器和标签以外的地方访问配置(例如 BootStrap )。如果你在一个支持依赖注入的工件中需要它,简单地注入

解决方案

    code> grailsApplication

      class MyController {
    def grailsApplication

    def myAction = {
    def bar = grailsApplication.config.my.property
    }
    }

    如果您在bean中需要 src / groovy 或 src / java ,使用 conf / spring / resources.groovy $ b

      // 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')
    //或使用'autowire'
    }
    }

    $ b $其他任何地方,最简单的方法是将配置对象传递给需要它的类,或传递所需的特定属性。



    <$ p $ b $ // src / groovy / com / example / NotABean.groovy $ b $ class NotABean {
    def foo(def bar){
    ...
    }
    }

    //由DI支持的工件调用
    class MyController {
    def grailsApplication
    def myAction = {
    def f = new NotABean()
    f.foo(grailsApplication.config.my.property)
    }
    }




更新:
$ b 最近撰写了一些关于此的博客文章。 getDomainClass()从在域类中,而另一个提供的选项(如果没有上述解决方案是适当的)。

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

解决方案

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

这篇关于如何在Grails 2.0中访问Grails配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:14