Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting grails 2.0.0M1 config info in domain object, and static scope?

How do I get to the Config.groovy information from a domain object, or from a static scope? I'm using ConfigurationHolder.config.* now, but that and ApplicationHolder are deprecated so I'd like to 'do it right' ... but the grailsApplication object isn't available in a DO/static scope.

like image 588
Wayne Avatar asked Aug 08 '11 01:08

Wayne


2 Answers

The Grails 2 replacement for the deprecated ApplicationHolder, ConfigurationHolder, etc. is grails.util.Holders, which provides the same functionality but in a way that is safe when several different webapps in the same container are sharing a single copy of the Grails JARs in a parent classloader (this being the case where the old holders broke down).

import grails.util.Holders

// ...

static void foo() {
  def configOption = Holders.config.myapp.option
}
like image 75
Ian Roberts Avatar answered Oct 09 '22 15:10

Ian Roberts


I'd add the grailsApplication to the metaclass of domain classes - this is something I'm thinking about doing for 2.0 final. For now, put it in BootStrap.groovy, e.g.

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      for (dc in grailsApplication.domainClasses) {
         dc.clazz.metaClass.getGrailsApplication = { -> grailsApplication }
         dc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication }
      }      
   }
}

Then you can access the config from grailsApplication.config, and Spring beans via grailsApplication.mainContext.getBean('foo') or just grailsApplication.mainContext.foo.

like image 24
Burt Beckwith Avatar answered Oct 09 '22 15:10

Burt Beckwith