2011-08-20 75 views
63

我獲得了最新的Grails 2.0的里程碑,我看到了ConfigurationHolder類棄用警告:如何在Grails 2.0中訪問Grails配置?

org.codehaus.groovy.grails.commons.ConfigurationHolder 

過時消息,只是說「使用依賴注入,而不是」這是不是對我很有幫助。我理解依賴注入,但是我怎樣才能用適當的Grails配置連接bean,以便在運行時訪問它?我需要從我的控制器和標籤以外的地方訪問配置(例如BootStrap)。

回答

101
  • 如果你需要在支持依賴注入的神器,只需注入grailsApplication

    class MyController { 
        def grailsApplication 
    
        def myAction = { 
         def bar = grailsApplication.config.my.property 
        } 
    } 
    
  • 如果你需要在一個bean ,說,src/groovysrc/java,電線它使用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' 
        } 
    } 
    
  • 其他任何地方,最簡單的方法是將配置對象傳遞給需要它的類,或傳遞所需的特定屬性。

    // 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) 
        } 
    } 
    

更新:

Burt Beckwith最近寫了一對夫婦的博客文章在此。 One discusses usinggetDomainClass(),而另一個提供creating your own holder class的選項(如果上述解決方案都不合適)。

+0

Rob,非常有幫助的答案。謝謝。 – Polaris878

18

你可以在源文件中注入「grailsApplication」。這裏是一個樣本的conf/BootStrap.groovy中

class BootStrap { 

    def grailsApplication 

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

    def destroy = { 
    } 
} 
+0

+1,非常好的建議! – Polaris878

10

另一個非過時的方式獲得的配置是:

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

這工作在情況下有沒有注入母公司提供,如servlet類或靜態方法。

56

到grailsApplication另一種是Holders類,

import grails.util.Holders 

def config = Holders.config 

您可以直接獲取配置關持有人,沒有注射需要,這是工具類等不錯

+0

這可能是最直接的方法,因爲直接與棄用類進行1:1映射。 – Andrew

+1

何時'grails.util.Holders'更適合注入'grailsApplication'? –

+2

@AlexanderSuraphel當你不想讓Spring控制bean的生命週期時。例如,一個具有公共靜態方法的實用程序類。 –

4

您可以訪問Grails的配置

  1. 在控制器

    class DemoController { 
        def grailsApplication 
    
        def demoAction = { 
         def obj = grailsApplication.config.propertyInConfig 
        } 
    } 
    
  2. 在服務方面:

    class DemoService { 
        def grailsApplication 
    
        def demoMethod = { 
         def obj = grailsApplication.config.propertyInConfig 
        } 
    } 
    
  3. 在標籤庫:

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

可以調用鑑於標籤庫的此方法作爲<cd:demoMethod/>

  • 鑑於:

    <html> 
        <head><title>Demo</title></head> 
    <body> 
        ${grailsApplication.config.propertyInConfig} 
    </body> 
    
    </html> 
    
  • 0

    要從域類訪問做:

    import grails.util.Holders 
    
    // more code 
    
    static void foo() { 
        def configOption = Holders.config.myProperty 
    }