How to Solve in Grails 2.x: ConfigurationHolder.getConfig() is Deprecated

With the newer Grails version 2.x, ConfigurationHolder is marked as deprecated.

In Grails apps ver. < 2.0, you can access your configuration properties:

// BootStrap.groovy

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
class BootStrap {
....
   // e.g.: def <propertyValue> = CH.<property>
   def serverURL = CH.grails.serverURL
}

To avoid the deprecated class ConfigurationHolder in Grails ver. >= 2.0, use the following dependency injection grailsApplication:

// BootStrap.groovy

class BootStrap {
   def grailsApplication

   def init = {servletContext ->
       // e.g.: def <propertyValue> = grailsApplication.config.<property>
      def serverURL = grailsApplication.config.grails.serverURL
   }
}

Custom properties can be defined in Config.groovy:

// Config.groovy
...
environments {
   development {
     grails.serverURL = "http://localhost:8080/${appName}"
   }

   production {
      grails.serverURL = "http://www.changeme.com"
  }
}
Advertisements
This entry was posted in Development, Grails and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s