Search code examples
grails

GrailsApplication or Holders in Grails 6


I'm trying to develop an application using Grails 6.1.0, I need to have access to the application.yml to retrieve some configs from it, when I'm using GrailsApplication like the below example, it always gets me Cannot get property 'config' on null object, however when I use Holders.config.getProperty("environments.dataSource.url"), I get the property value successfully, is this the right way to access the configs in any part of my Grails application or there is a better way

import grails.core.*

class MyService {
GrailsApplication grailsApplication

String greeting() {
    def recipient = grailsApplication.config.getProperty('foo.bar.hello')
    return "Hello ${recipient}"
}

}


Solution

  • In grails6, use the grails.util.Holders

    import grails.util.Holders;
    
    public class Example {
      void someMethod() {
         String appName = Holders.getConfig().getProperty("info.app.name");
    
      }
    }