How to access Grails configuration in Grails 2.0?
Asked Answered
C

7

65

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

Cosmorama answered 20/8, 2011 at 17:55 Comment(0)
B
105
  • 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).

Bowra answered 21/8, 2011 at 3:14 Comment(0)
E
59

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.

Enisle answered 5/4, 2013 at 3:27 Comment(4)
This is probably the most straightforward way, since there's a direct 1:1 mapping to the deprecated class.Peroration
When is grails.util.Holders preferable to injecting grailsApplication?Aiguille
@AlexanderSuraphel when you don't want to have Spring control the lifecycle of your bean. For instance, a utility class with public static methods.Enisle
how can we use it in external javascript file?Crandell
C
18

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 = {
    }
}
Centeno answered 20/8, 2011 at 22:7 Comment(0)
M
10

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.

Maori answered 22/5, 2012 at 13:14 Comment(0)
P
4

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>
    
Puerility answered 2/9, 2014 at 10:32 Comment(0)
D
0

To access from domain class do:

import grails.util.Holders

// more code

static void foo() {
  def configOption = Holders.config.myProperty
}
Delisle answered 24/8, 2017 at 23:24 Comment(0)
T
0
import grails.util.Holders

and then you can access by 
def serverUrl = Holders.grailsApplication.config.getProperty('serverUrl')
Trantrance answered 19/12, 2022 at 11:59 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Desma

© 2022 - 2024 — McMap. All rights reserved.